layout layout_weight和wrap_content

时间:2018-05-26 09:46:55

标签: android xml layout

对于android:layout_weight="1" , android:layout_height="0dp"

的一个容器中的两个元素,使用(android:layout_height="wrap_content")和(android:weightSum="1")是否标准

我检查了android 4 6 8 ,我没有看到任何问题 但我的问题是:

如果其中一个元素感觉到整个weightSum其他元素是如何可见的那样?

我使用此布局在布局中有一个可见且不可见的底部菜单。

像这样:

<LinearLayout
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    >

 <LinearLayout
 android:orientation="vertical" 
 android:layout_weight="1"
 android:layout_width="match_parent"
 android:layout_height="0dp">
 </LinearLayout>

 <LinearLayout
 android:orientation="vertical" 
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ok"
                    />
 </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

重量表示REMAINING空间中的指定值...表示您的视图为1重量,因此每个剩余空间都会占用您的视图...(剩余意味着全视图考虑它们的宽度屏幕高度以及剩余空间后剩余空间在屏幕中......按照你的线性布局计算),所有空间都考虑你的1重量

refer this official document

答案 1 :(得分:1)

  

我没有看到任何问题,但我的问题是,如果有一个元素感觉到   整个weightSum如何看到其他元素?

它实际上并没有使用整个权重。将android:layout_weight="1"的高度设置为0意味着它将首先检查是否有任何其他布局要使用一些定义为wrap_content的高度,然后在存在的任何位置之后,它将使目标的剩余高度首先布局。