Deep understanding of Layout_weight in LinearLayout layout

First look at the role of the Layout_weight attribute in the LinearLayout layout: it is used to assign an attribute that belongs to the space, and you can set its weight. Many people do not know what the concept of remaining space is. Let me talk about the remaining space first.

See the code below:

<?xml version="1.0" encoding="utf-8"?>     
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    android:orientation="vertical"     
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent"     
    >     
<EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="left"     
    android:text="one"/>     
<EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="center"     
    android:layout_weight="1.0"     
    android:text="two"/>     
    <EditText     
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
    android:gravity="right"     
    android:text="three"/>     
</LinearLayout>     

 

The result of running is:

Look at the above code: only Button2 uses the Layout_weight attribute and assigns it to 1, while Button1 and Button3 do not set the Layout_weight attribute. According to the API, they default to 0.

Let me talk about the real meaning of the attribute Layout_weight: the Android system first assigns you the height of the three buttons according to the Layout_height value wrap_content of the three Button heights you set,

Then all the remaining screen space will be assigned to Button2, because only his weight value is 1, which is why Button2 occupies such a large space.

With the above understanding, we can have a clear understanding of the more puzzling effect of the Layout_weight attribute on the Internet.

Let's look at this code:

 <?xml version="1.0" encoding="UTF-8">   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:orientation="horizontal" >   
    <TextView   
        android:background="#ff0000"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="1"   
        android:textColor="@android:color/white"   
        android:layout_weight="1"/>   
    <TextView   
        android:background="#cccccc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="2"   
        android:textColor="@android:color/black"   
        android:layout_weight="2" />   
     <TextView   
        android:background="#ddaacc"   
        android:layout_width="**"   
        android:layout_height="wrap_content"   
        android:text="3"   
        android:textColor="@android:color/black"   
        android:layout_weight="3" />   
</LinearLayout> 

 

三个文本框的都是 layout_width=“wrap_content 时,会得到以下效果

按照上面的理解,系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容1,2,3即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,所以就出现了上面的图像。

而当layout_width=fill_parent时,如果分别给三个TextView设置他们的Layout_weight为1、2、2的话,就会出现下面的效果:

你会发现1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=fill_parent时,weighth值越小权重越大,优先级越高,就好像在背口诀

一样,其实他们并没有真正理解这个问题,真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我们来分析:

系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度

那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )

那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width

同理第二个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;

第三个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。

这样你也就会明白为什么当你把三个Layout_weight设置为1、2、3的话,会出现下面的效果了:

第三个直接不显示了,为什么呢?一起来按上面方法算一下吧:

系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度

那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )

那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)=2/3parent_width

同理第二个TextView的实际所占宽度=parent_width + 2/6*(-2parent_width)=1/3parent_width;

第三个TextView的实际所占宽度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326615348&siteId=291194637