Understanding android:layout_weight

99封情书 提交于 2019-12-23 20:04:51

问题


Why the following listing shows only the second TextView (red)?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="11111"
        android:background="#00FF00" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0"
        android:background="#FF0000"
        android:text="00000"/>
</LinearLayout>

I know that if I set android:layout_height="0px" it will only show the first TextView (green), and I understand this behavior.

But why when I set android:layout_height="match_parent", the first TextView disappear completely from the screen.


回答1:


From https://developer.android.com/guide/topics/ui/layout/linear.html

It disappers because it second one acquires full space as it is given

android:layout_weight="0"

and

android:layout_height="match_parent"** as mentioned in above link.

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured.

If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.




回答2:


android:layout_weight="1" means you are assigning the remaining space which is not occupied by other views to that view.. so here in your case second TextView is match_parent so no space is left blank thats why first TextView is not visible

P.S: Pass weightsum to the parent layout and distribute that weight among child Views according to your need.
Thanks




回答3:


android:layout_weight means how u want these views to be showed comparing to each other and the parent view for exampel, if u these codes:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1"/>
    </LinearLayout>

the result will be this image

if i change android:layout_weight of Button1 from 1 to 2 while Button2's weight is still 1, then Button1's Width will be 2times bigger than Button2's




回答4:


Your code is basically saying, give my first text view all the space and my second text view none, try giving the children each a weight of 1, this should give them equal spacing. Also as you have a vertical oriented parent, setting the layout height in the children to match the parent is contradicting to the weight attribute. So remove it and replace with wrap content.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="11111"
    android:background="#00FF00" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#FF0000"
    android:text="00000"/>
  </LinearLayout>



回答5:


https://developer.android.com/guide/topics/ui/layout/linear.html https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

https://ugia.io/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/

match_parent is different from wrap_content or specific sizes(0px, 1px, 100px etc.), it will take all space of parent view till the size of LinearLayout in your case.

layout_weight is for balancing, and it depends on your layout_width




回答6:


But why, if I set in the second TextView:

android: layout_weight = "0.1" 

the second component of the TextView will take less space than if I set:

android: layout_weight = "0"

And if I set in the second TextView

android: layout_weight = "0.2"

the second component of the TextView will take even less space than if I set

android: layout_weight = "0.1"

So if I set higher the value of android:layout_weight, that it occupies a smaller size.




回答7:


You have set the weight value of the first button to 1 and the second to 0, so button one will cover the whole space

You should give a weight total to the parent layout and distribute the total between the children according to the desired proportion.



来源:https://stackoverflow.com/questions/39508909/understanding-androidlayout-weight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!