Arno_C
Arno_C

Reputation: 119

nested weight in linear layout

I get a warning saying: "Nested weights are bad for performance". I've literally copied this code from another layout, but in this one it gives an error and in the other one it doesn't.

<?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 error but can somebody please point out what I'm doing wrong?

Upvotes: 6

Views: 8373

Answers (4)

Lorne K
Lorne K

Reputation: 109

If anyone else wishes to ignore NestedWeights, keep in mind you must also specify the tools in the header. For example

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

Upvotes: 0

Harry
Harry

Reputation: 379

<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 Nested Weight problem which reduce the performance of layout.

Upvotes: 0

Chirag Patel
Chirag Patel

Reputation: 11508

3 things to remember:

  • set the android:layout_width of the children to "0dp"

  • set the android:weightSum of the parent

  • set the android:layout_weight of each child proportionally (e.g. weightSum = "5", three children: layout_weight="1", layout_weight="3", layout_weight="1")

Upvotes: 5

Wietse de Vries
Wietse de Vries

Reputation: 685

You have to add weightSum to the linearlayout and remove the weight of the listview:

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

Upvotes: 7

Related Questions