android - 布局中的嵌套权重

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

我有一个布局,我希望组件填满屏幕的剩余部分(使用权重)。 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>

最佳答案

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

<?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> 

当您放置两个 View 时 1. ListView 2.线性布局(两个按钮) 您需要赋予权重属性以正确调整它们,对于按钮的水平除法,还需要为第二个线性布局赋予一些权重和属性,使用权重将其平均划分。请注意,在第二个线性布局中,我将按钮的宽度设置为 0px

关于android - 布局中的嵌套权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700336/

相关文章:

android - 在android中的 ImageView 中显示矩阵

android - 如何将一个 View 显示为另一个 View 的叠加层?

android - 以编程方式在(线性)布局(在 ScrollView 内)中添加 View

android - 当回收器 View 项目根是 LinearLayout 时,为什么 match_parent 不起作用?

java - 如何在 Android 上安装 JqMath?

android - 如何在禁用 USB 调试时使用 ADB 或任何其他方式打开 wifi

带有 layer_type_software 的 Android WebView 不显示 HTML5 Canvas 内容

java - 如何检测空白区域的点击?

java - 在运行时调整 android 线性布局的大小无法正常工作

android - 如何在以编程方式添加到 LinearLayout 的按钮之间添加边距空间?