为什么下面的 list 只显示第二个 TextView
(红色)?
<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>
我知道如果我设置 android:layout_height="0px"
它只会显示第一个 TextView
(绿色),我理解这种行为。
但为什么当我设置 android:layout_height="match_parent"
时,第一个 TextView
完全从屏幕上消失了。
最佳答案
来自 https://developer.android.com/guide/topics/ui/layout/linear.html
它消失了,因为它的第二个获得了给定的完整空间
android:layout_weight="0"
和
android:layout_height="match_parent"
** 如上面的链接所述。
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.
关于android - 了解安卓 :layout_weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39508909/