线性布局中的嵌套权重

6

我收到一个警告: "嵌套权重对性能有害"。我刚刚从另一个布局中复制了这段代码,但在这个布局中它会出现错误,在另一个布局中则不会。

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

    <ListView
        android:id="@+id/listWeighings_weighing"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:background="#000000" >
    </ListView>

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

        <Button
            android:id="@+id/btnInsertMutation_weighing"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="Mutatie invoeren" />

        <Button
            android:id="@+id/btnExecuteWeighing_weighing"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="weging uitvoeren" />
    </LinearLayout>

</LinearLayout>

我敢打赌这是一个愚蠢的错误,但有人能指出我错在哪里吗?

你在哪一行遇到这个错误? - Wietse de Vries
你发布的布局是正确的。我认为它没有错误。如果Eclipse仍然显示有错误,请尝试清理项目。 - Luis Ollero
这只是一个警告,你可以忽略它... ;) - Sam Dozor
1
请参考以下链接以优化布局:http://developer.android.com/training/improving-layouts/optimizing-layout.html - Muhammad Babar
4个回答

7
你需要在linearlayout中添加weightSum并移除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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listWeighings_weighing"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        android:background="#000000" >
    </ListView>

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

        <Button
            android:id="@+id/btnInsertMutation_weighing"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="Mutatie invoeren" />

        <Button
            android:id="@+id/btnExecuteWeighing_weighing"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="weging uitvoeren" />
    </LinearLayout>

</LinearLayout>

5

需要记住的三件事:

  • 将子元素的android:layout_width设置为"0dp"

  • 设置父元素的android:weightSum

  • 按比例设置每个子元素的android:layout_weight(例如,weightSum = "5",三个子元素:layout_weight="1",layout_weight="3",layout_weight="1")

0
如果有人想忽略NestedWeights,请记住您必须在标头中指定工具。例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/appDarkBackGround"
android:orientation="horizontal"
tools:context=".MainActivity" 
tools:ignore="NestedWeights"
>

忽略有关性能的警告并不能解决根本问题。 - garritfra

0
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@color/black"
    android:orientation="horizontal"
    tools:ignore="NestedWeights" >

添加此行以忽略嵌套权重问题,从而提高布局性能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接