嵌套布局权重

时间:2013-10-16 18:36:19

标签: android layout

我正在尝试设计我的UI: enter image description here

问题是,当我添加ListView并将其宽度设置为match_parent时,它使用根布局并获取整个屏幕的宽度,即使它包装在另一个布局中,只占宽度的1/4。有人可以建议我如何完成图片上的最新情况吗?我还读到嵌套的layout_weight对性能有害,我应该使用完全不同的东西吗?

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"> 
    </RelativeLayout>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="13">

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </ListView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_weight="1">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

看起来你的ViewGroup太多View了。您可以更改此部分

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_weight="13">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

类似

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

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

我认为不需要额外的LinearLayout。如果您需要LinearLayout的某些属性,例如RelativeLayoutButton

,您也可以将此layout_alignParentBottom更改为layout_above

答案 1 :(得分:0)

将您的右侧视图更改为RelativeLayout,将您的按钮设置在右侧视图的底部,然后在android:layout_above="@id/button1"上设置属性ListView将允许您可以让ListView填充按钮不占用的剩余空间。我还删除了许多不需要的嵌套布局。

不要忘记,在使用布局权重时,您需要为宽度/高度指定0dip,具体取决于LinearLayout的方向。这可能是您的ListView占据整个屏幕的原因。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="3" />

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/button1" />
    </RelativeLayout>

</LinearLayout>
相关问题