Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 579
Arno_C Avatar asked Mar 08 '12 15:03

Arno_C


People also ask

What are nested weights?

The idea of nesting weights, which dates back to Roman times, was to increase the portability of weight sets by compacting an entire group of weights into a container. In the nested cup form, a series of weights shaped into cups are set one into the other, forming a stack which is stored within a house vessel.

How is weight used in linear layout?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

What is weight sum in linear layout?

If there are 2 views in the LinearLayout, the first with a layout_weight of 1, the second with a layout_weight of 2 and no weightSum is specified, by default, the weightSum is calculated to be 3 (sum of the weights of the children) and the first view takes 1/3 of the space while the second takes 2/3.

Can linear layout be nested in relative layout?

Linear Layout can be used inside relative layout since one layout can be nested in other layout in XML.


2 Answers

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>
like image 117
Wietse de Vries Avatar answered Sep 21 '22 12:09

Wietse de Vries


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")
like image 39
Chirag Patel Avatar answered Sep 23 '22 12:09

Chirag Patel