Nested weight in linear layout

I get a warning: "Nested weights are bad for performance". I literally copied this code from another layout, but it gives an error in this, but not in the other.

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

I bet it's a stupid mistake, but can someone point out what I'm doing wrong?

+6
source share
4 answers

You must add weightSum to linearlayout and remove the list weight:

<?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>
+7
source

3 things to remember:

  • set android: layout_width for children to "0dp"

  • install android: parentS weightSum

  • android: layout_weight (, weightSum = "5", : layout_weight = "1", layout_weight = "3", layout_weight = "1" )
+4
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@color/black"
    android:orientation="horizontal"
    tools:ignore="NestedWeights" >

Add this line to ignore the nested weight problem that reduces layout performance.

+1
source

If anyone else wants to ignore NestedWeights, keep in mind that you should also specify the tools in the header. for instance

<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"
>
+1
source

All Articles