布局中的嵌套权重

时间:2012-03-14 10:41:10

标签: android android-layout android-linearlayout android-layout-weight

我有一个布局,我希望组件填满屏幕的剩余部分(使用权重)。 Listview非常有效。

最后的两个按钮的工作方式也是我打算工作的方式,但是我收到一条警告,上面写着“嵌套权重对性能不利”。

我认为这是一个问题因为我已经为Listview使用了一个权重参数,因为当我删除listview时警告就消失了。

    <?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="fill_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listMessages_messages"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="5dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip"
            android:layout_weight="1"
            android:background="#000000" >
        </ListView>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:visibility="invisible" />

            <Button
                android:id="@+id/btnNewConversation_message"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="50"
                android:onClick="newConversation"
                android:text="@string/NewConversation" />
        </LinearLayout>

    </LinearLayout>

2 个答案:

答案 0 :(得分:1)

检查此代码。我没有收到警告

<?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="fill_parent"
    android:orientation="vertical"
    android:weightSum="10">
    <ListView android:id="@+id/listMessages_messages"
        android:layout_width="fill_parent" android:layout_height="0px"
        android:layout_marginBottom="5dip" android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip" android:layout_marginTop="10dip"
        android:layout_weight="8" android:background="#000000">
    </ListView>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="0px" android:orientation="horizontal"
        android:weightSum="10" android:layout_weight="2">
        <Button android:layout_width="0px"
            android:layout_height="wrap_content" android:layout_weight="5"
             />
        <Button android:id="@+id/btnNewConversation_message"
            android:layout_width="0px" android:layout_height="wrap_content"
            android:layout_marginRight="5dip" android:layout_weight="5"
            android:onClick="newConversation"  />
    </LinearLayout>
</LinearLayout> 

当您放置两个视图时 1.列表视图 2.线性布局(两个按钮) 你需要给予权重属性来适当地调整它们,而对于按钮的水平分割也给予第二个线性布局一些权重和属性,使用权重将它们平均分配。注意在第二个线性布局中我使用按钮的宽度为0px

答案 1 :(得分:0)

如果我理解你的问题,以下是sol:  (比如说10) 在顶部布局中为ListView提供一些权重,如5或6 确保将layout height属性设置为0px 创建子线性布局给出剩余权重(即列表视图的10重量) 作为子布局的layout_weight,子布局layout_height为0px 在这个布局中放置两个按钮。希望这是你需要的