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

2023-12-14

我想显示一个有组织的网格。所以我使用 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>

请问我怎样才能避免这个错误?


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

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

这也可以在 Eclipse 中完成,方法是将光标放在黄线上并按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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌套权重对我的 XML 代码的性能不利[重复] 的相关文章

随机推荐