Nested weights are bad for performance in my XML code [duplicate]

喜夏-厌秋 提交于 2019-12-01 12:43:06

You can only ignore this warning if you want to keep this layout.

Add to the root of your layout: xmlns:tools="http://schemas.android.com/tools". Then in your buttons add tools:ignore="NestedWeights".

This can be also done in eclipse by putting the cursor on the yellow line and pressing ctrl + 1. You can choose ignore.


If you want to improve performance, you can use a 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>

This occurs because you could have performance impact while rendering the component, to be accomodate Layout to fix in parent a letter Button to fix accordingly Buttons to space of LinearLayout,just remove layout_weightof the LinearLayout

One way to avoid this warning is to flatten your layout by replacing the outer LinearLayout with a RelativeLayout.

The other thing that pops out to me is that the first child LinearLayout is completely unnecessary. It has only one child (another LinearLayout), so it isn't really doing anything.

With a RelativeLayout, you can simplify this layout to:

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!