вложенный вес в линейной компоновке - PullRequest
6 голосов
/ 08 марта 2012

Я получаю предупреждение: "Nested weights are bad for performance". Я буквально скопировал этот код из другого макета, но в этом он выдает ошибку, а в другом - нет.

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

Могу поспорить, что это глупая ошибка, но может кто-нибудь указать, что я делаю неправильно?

Ответы [ 4 ]

7 голосов
/ 08 марта 2012

Вы должны добавить weightSum к линейной разметке и удалить вес списка:

<?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>
4 голосов
/ 10 марта 2012

3 вещи, которые нужно запомнить:

  • установить андроид: layout_width детей в "0dp"

  • установить андроид: весовая сумма родителя

  • установить android: layout_weight каждого ребенка пропорционально (например, weightSum = "5", трое детей: layout_weight = "1", layout_weight = "3", layout_weight = "1")
1 голос
/ 08 октября 2014

Если кто-то еще хочет игнорировать NestedWeights, имейте в виду, что вы также должны указать инструменты в заголовке. Например

<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 голос
/ 21 января 2013
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@color/black"
    android:orientation="horizontal"
    tools:ignore="NestedWeights" >

Добавьте эту строку, чтобы игнорировать проблему Nested Weight, которая снижает производительность макета.

...