嵌套权重对我的XML代码的性能不利

时间:2013-10-28 13:39:08

标签: android xml android-layout-weight

我想显示一个有组织的网格。所以我使用LinearLayout和Layout Weight选项,每件事情都很完美,但我不明白如何在按钮A和按钮C中避免这个警告:

Nested weights are bad for performance

这是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

        <!-- First row. -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="A" />

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="B" />
        </LinearLayout>

        <!-- Second row. -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2" >

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="D" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我该如何避免这个错误?

3 个答案:

答案 0 :(得分:21)

如果要保留此布局,则只能忽略此警告。

添加到布局的根目录:xmlns:tools="http://schemas.android.com/tools"。然后在您的按钮中添加tools:ignore="NestedWeights"

这也可以通过将光标放在黄线上并按ctrl + 1来完成。您可以选择忽略。


如果要提高性能,可以使用TableLayout

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="5dip" >

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="A" />

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="B" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="5dip" >
            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:layout_width="0dip"
                android:layout_height="fill_parent"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="D" />
    </TableRow>
</TableLayout>

答案 1 :(得分:0)

发生这种情况是因为渲染组件时可能会影响性能,可以容纳布局以修复父级字母按钮以相应地修复按钮到LinearLayout的空格,只需删除LinearLayout的layout_weight

答案 2 :(得分:0)

避免此警告的一种方法是使用RelativeLayout替换外部LinearLayout来展平您的布局。

对我而言,另一件事是第一个孩子LinearLayout完全没必要。它只有一个孩子(另一个LinearLayout),所以它实际上并没有做任何事情。

使用RelativeLayout,您可以将此布局简化为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/row1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="A" />

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="B" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/row2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/row1"
        android:weightSum="2" >

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="C" />

        <Button
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="D" />
    </LinearLayout>

</RelativeLayout>