问题
I want to display an organized grid. So I use LinearLayout and Layout Weight option, every thing works perfectly, but I don't understand how to avoid this warning in the Button A and the Button C:
Nested weights are bad for performance
And this is my XML code :
<?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>
How can I avoid this error please ?
回答1:
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>
回答2:
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_weight
of the LinearLayout
回答3:
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>
来源:https://stackoverflow.com/questions/19636323/nested-weights-are-bad-for-performance-in-my-xml-code