android浮动布局

1.

gravity:使内部元素浮动。

一个view(TextView,ImageView,ImageButton...)就是一个div,不过它不能向div那样具有flow:left|right等属性,如非要用浮动定位的话,可以添加第三方依赖,用法如下:

//build中添加依赖
compile 'com.nex3z:flow-layout:1.0.0'

xml中布局:
<com.nex3z.flowlayout.FlowLayout
    android:id="@+id/flow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:flChildSpacing="7dp"
    app:flRowSpacing="8dp" />

属性有:
FlowLayout.LEFT                ——左对齐

FlowLayout.RIGHT             ——右对齐

FlowLayout.CENTER—–居中对齐

FlowLayout.LEADING ——与开始一边对齐

FlowLayout.TRAILING——-与结束一边对齐


 



但android布局中常用的居中,居右,居左的处理方法是:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="#242424">

        <TextView
            android:onClick="closedAc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="关闭" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:gravity="center"
            android:paddingLeft="-100dp"
            android:text="工大测试" />


    </LinearLayout>

 

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:background="#242424"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭"
            android:gravity="right"
            android:layout_weight="0.3"
            android:textColor="#ffffff"
            android:onClick="closedAc"
            android:id="@+id/button2"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="收音"
            android:gravity="center"
            android:layout_weight="0.3"
            android:textColor="#ffffff"
            android:onClick="closedB"
            />
        <TextView
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:text="讲话"
            android:gravity="left"
            android:layout_weight="0.3"
            android:textColor="#ffffff"
            android:onClick="closedC"
            />
    </LinearLayout>

 

LineaLayout的排列方式是horizontal时,layout_gravity只在垂直方向上生效,因为此时水平方向的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同样的道理,当LineaLayout的排列方式为vertical时,layout_gravity只在水平方向上的对齐方式才会生效。

总结:

 <LinearLayout>中代码
        android:orientation="horizontal"
        android:gravity="center_vertical"

 <xxxView>中代码:

            android:gravity="right"
            android:layout_weight="0.3"

主要是以上几行代码实现布局

2.android:layout_weight 这个属性代表了一个“重要性”的值,这个值的大小代表了该控件能在屏幕中占据多大的空间。这个值越大,表明该控件可以在父控件中占据较多的“ 剩余 ”空间。默认的weight是0。最小权重是0,最大是1


 

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/82924259