Why is Android lint showing “nested weights are bad for performance” when none are nested?
-
30-05-2021 - |
Вопрос
The layout_weight attribute in the last TextView in this layout is being highlighted by lint as causing bad performance. I suspect the name of the lint warning is erroneous as there are no nested weights. There are several stretching views, and I suspect what it means is dependent weighted views.
What's the solution, to build this layout, and avoid this lint warning, and improve performance?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="@drawable/background_gradient"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image4"
android:layout_margin="6dp"
android:contentDescription="@string/PLACEHOLDER_TEXT"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
style="@style/dashboard_button"
android:id="@+id/home_management"
android:text="@string/ICON_MANAGEMENT"
/>
<Button
style="@style/dashboard_button"
android:id="@+id/home_calculators"
android:text="@string/ICON_CALCULATORS"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
style="@style/dashboard_button"
android:id="@+id/home_guidelines"
android:text="@string/ICON_GUIDELINES"
/>
<Button
style="@style/dashboard_button"
android:id="@+id/home_faq"
android:text="@string/ICON_FAQ"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
style="@style/dashboard_button"
android:id="@+id/home_refs"
android:text="@string/ICON_REFERENCES"
/>
<Button
style="@style/dashboard_button"
android:text=""
android:visibility="invisible"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_margin="6dp"
>
<TextView
android:text="@string/HOMEPAGE_CONTENT"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold"
android:textColor="#FFFFFF"
/>
<View
android:layout_width="8dp"
android:layout_height="1dp"
/>
<ImageView
android:id="@+id/home_logo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/image7"
android:contentDescription="@string/PLACEHOLDER_TEXT"
/>
</LinearLayout>
<Button
android:id="@+id/home_web_site"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/HOMEPAGE_FOOTER"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textSize="12sp"
android:background="@drawable/button_red"
android:layout_margin="6dp"
android:textColor="#FFFFFF"
/>
</LinearLayout>
Решение
The TextView
and ImageView
in your @string/HOMEPAGE_CONTENT
area are nested in their parent LinearLayout
and have weights assigned. I suspect those are your problem, especially since the parent is wrap_content
, so theoretically there shouldn't be any space for the child elements to stretch into.
Другие советы
You don't need to worry about this, however, especially if you really need the weights, and if that's the only way to declare your layout properly.
Concerning this part of code.
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" <Button style="@style/dashboard_button" android:id="@+id/home_management" android:text="@string/ICON_MANAGEMENT" /> <Button style="@style/dashboard_button" android:id="@+id/home_calculators" android:text="@string/ICON_CALCULATORS" />
Your LinearLayout contains "layout_weight" attribute, and if the style "dashboard_button" contains the attribute "layout_weight" too, then you got couple of nested views with the "layout_weight" attribute, though the lint doesn't warn me now concerning "layout_weight" inside the styles. My solution is to substitue one(or both) of the LinearLayouts with RelativLayout and adjust the sizes and places programmatically.