Android布局之LinearLayout weight属性的理解

android:layout_weight 此属性严格的说法应该是对当前剩余空间按权重平分


我们平时常会用到android:layout_weight 这个属性来对UI空间进行百分比划分,一个是使界面看起来比较整齐,还有就是比较容易调整。

根据android:layout_weight 设置的实例:
实例

布局文件代码如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/main_i_edittext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="8"
        android:background="@drawable/rounded_edittext"
        android:gravity="center"
        android:singleLine="true"
        android:clickable="true"
        android:focusable="true"
        android:onClick="main_click_i"
        android:text="@string/default_value_0"
        android:textColor="#000000"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/main_i_textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="2"
        android:singleLine="true"
        android:gravity="bottom"
        android:text="A"
        android:textColor="#00f"
        android:textSize="17sp" />
</LinearLayout>

这里我采用了最常用的0dp作为Texview的宽度,下面我们来看一下三种不同的宽度参数对权重参数的影响
借用一张以前看到的图,^_^
示例

每个LinearLayout内部三个TextView的layout_weight分别为1,2,3。由于其wrap_content的不同,使其layout_weight的分配受到了影响。这里就来layout_weight按屏幕剩余空间按权重分配的情况。

第一种情况(android:layout_height=”wrap_content”)
系统给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,
上面的UI 比重为 :6*1/6 ,6*2/6,6*3/6 即1:2:3 ,如UI 第一行呈现的那样。

第二种情况(android:layout_height=” match_parent “)
系统先给3个textview分配他们所要的宽度match_parent,也就是说每一都是填满他的父控件。

假设屏幕宽度为1。那么三个控件占据后的剩余空间就是1-1*3=-2;然后将剩余的-2宽度屏幕控件按照1:2:3的比例分给三个控件。
A:-2*(1/6)=-1/3 ; 然后原来分配给控件的宽度为1;1+(-1/3)=2/3;故实际宽度为2/3
B:-2*(2/6)=-2/3 ;同上,1+(-2/3)=1/3;故实际宽度为1/3
C:-2*(3/6)=-1;同上,1+(-1)=0;故实际宽度为0,这也就是为什么C显示不出来的原因了

第三种情况(android:layout_height=” 0dp”)
这种情况,不管TextView的内容宽度,直接将屏幕剩余空间按照1:2:3的比例分配给三个TextView。UI 比重虽然也是1:2:3,但是与第一种情况还是有一点区别。


这里我贴的是第三种情况的代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#77bc1f"
        android:text="AAA"
        android:textColor="#fff"
        android:textSize="23sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#06d992"
        android:text="BBB"
        android:textColor="#fff"
        android:textSize="23sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:background="#EEA32E"
        android:text="CCC"
        android:textColor="#fff"
        android:textSize="23sp" />
</LinearLayout>

总结:

在使用android:layout_weight进行空间分配的时候我们要注意是对当前剩余空间按权重平分,觉得麻烦的话就设置组件宽度为0dp是最省心的了!

发布了13 篇原创文章 · 获赞 16 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_32969455/article/details/79403092