Android layout_weight 混乱(三个元素,一个应该长满屏幕)
Android layout_weight confusion (three elements, one should grow to fill the screen)
我想实现如下布局:
ListView LV1 应该包装它的内容并留在顶部。 LinearLayout LL3 应该包装它的内容并留在底部。 ListView LV2 应该填满剩余的屏幕。
只要 LV2 有足够的条目填满屏幕,它就可以正常工作。但是当 LV2 仅包含例如两个或零个元素时,LL3 向上移动。
为了让 LV2 始终填满屏幕而不管 ListView 中的元素数量,并让 LL3 保持在底部,我需要更改什么?
这是我的布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/LV1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="0"
android:drawSelectorOnTop="true"></ListView>
<ListView
android:id="@+id/LV2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="1"></ListView>
<LinearLayout
android:id="@+id/LL3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:id="@+id/button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Cancel"
/>
<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Save"
/>
</LinearLayout>
</LinearLayout>
由于您无法让 LL3
自行固定在底部,因此您应该将包装 LinearLayout
更改为 RelativeLayout
:
//pseudo code:
<RelativeLayout>
<LV1
android:alignParentTop="true"/>
<LV2
android:layout_below="@+id/lv1"
android:layout_above="@+id/ll3"/>
<LL3
android:alignParentBottom="true"/>
</RelativeLayout>
更好的方法是使用 RelativeLayout 而不是 LinearLayout。但您也可以使用 LinearLayout 实现这一点。请看下面的示例代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="@+id/LL1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/highlighted_text_dark"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/LL2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:background="@android:color/suggestion_highlight_text"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/LL3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/perms_costs_money"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
此代码的输出将是:
Parent LinearLayout的weightSum和children数量可以根据自己的需要修改
用得好weightSum
,你就会得到
检查这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="11"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/LV1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="2"
android:drawSelectorOnTop="true" />
<View //this view is added just to separate listiview
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<ListView
android:id="@+id/LV2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="8" />
<View //this view is added just to separate listiview
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<LinearLayout
android:id="@+id/LL3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Cancel" />
<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Save" />
</LinearLayout>
</LinearLayout>
如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则第三个没有权重的文本字段不会增长,只会占据其内容所需的区域。另外两个将同样扩展以填充所有三个字段测量后剩余的 space。如果第三个字段的权重为 2(而不是 0),那么它现在被声明为比其他两个都更重要,因此它获得剩余总数的一半 space,而前两个平分其余部分.
我想实现如下布局:
ListView LV1 应该包装它的内容并留在顶部。 LinearLayout LL3 应该包装它的内容并留在底部。 ListView LV2 应该填满剩余的屏幕。
只要 LV2 有足够的条目填满屏幕,它就可以正常工作。但是当 LV2 仅包含例如两个或零个元素时,LL3 向上移动。
为了让 LV2 始终填满屏幕而不管 ListView 中的元素数量,并让 LL3 保持在底部,我需要更改什么?
这是我的布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/LV1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="0"
android:drawSelectorOnTop="true"></ListView>
<ListView
android:id="@+id/LV2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="1"></ListView>
<LinearLayout
android:id="@+id/LL3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:id="@+id/button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Cancel"
/>
<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Save"
/>
</LinearLayout>
</LinearLayout>
由于您无法让 LL3
自行固定在底部,因此您应该将包装 LinearLayout
更改为 RelativeLayout
:
//pseudo code:
<RelativeLayout>
<LV1
android:alignParentTop="true"/>
<LV2
android:layout_below="@+id/lv1"
android:layout_above="@+id/ll3"/>
<LL3
android:alignParentBottom="true"/>
</RelativeLayout>
更好的方法是使用 RelativeLayout 而不是 LinearLayout。但您也可以使用 LinearLayout 实现这一点。请看下面的示例代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:id="@+id/LL1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/highlighted_text_dark"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/LL2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:background="@android:color/suggestion_highlight_text"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/LL3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:background="@android:color/perms_costs_money"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
此代码的输出将是:
Parent LinearLayout的weightSum和children数量可以根据自己的需要修改
用得好weightSum
,你就会得到
检查这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="11"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/LV1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="2"
android:drawSelectorOnTop="true" />
<View //this view is added just to separate listiview
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<ListView
android:id="@+id/LV2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="8" />
<View //this view is added just to separate listiview
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<LinearLayout
android:id="@+id/LL3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Cancel" />
<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Button_Save" />
</LinearLayout>
</LinearLayout>
如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则第三个没有权重的文本字段不会增长,只会占据其内容所需的区域。另外两个将同样扩展以填充所有三个字段测量后剩余的 space。如果第三个字段的权重为 2(而不是 0),那么它现在被声明为比其他两个都更重要,因此它获得剩余总数的一半 space,而前两个平分其余部分.