1

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

最后的 2 个按钮也按照我的意图工作,但是我收到一条警告,上面写着“嵌套权重不利于性能”。

我认为这是一个问题,因为我已经为 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>
4

2 回答 2

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.List 视图 2.Linear 布局(两个按钮)时,您需要提供权重属性以正确调整它们,并且对于按钮的水平划分,还需要为第二个线性布局提供一些权重总和属性,然后使用权重对其进行平均划分。注意在第二个线性布局中,我将按钮的宽度设为 0px

于 2012-03-14T11:35:22.690 回答
0

如果我正确理解了您的问题,则以下是 sol:(例如 10 个)在顶部布局中为 ListView 赋予一些权重,例如 5 或 6 确保将布局高度属性设置为 0px 创建子线性布局赋予剩余权重(即 10- listview 的权重)作为 layout_weight 到子布局的子布局 layout_height 为 0px,在这个布局位置有两个按钮。希望这是你需要的

于 2012-03-14T10:58:03.163 回答