1

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>
Ollie C
  • 28,313
  • 34
  • 134
  • 217

3 Answers3

2

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.

josephus
  • 8,284
  • 1
  • 37
  • 57
  • If we allow warnings in the build process, it's hard to know which warnings are "ok" and which are not. I prefer a zero-tolerance approach (either by fixing the warnings, or turning them off), then if a warning shows, it means it needs fixing. – Ollie C Apr 11 '12 at 09:36
1

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.

Argyle
  • 3,324
  • 25
  • 44
  • But aren't pretty much all weighted Views nested inside something? My reading of the warning is that it's saying one weighted view inside another weighted view is bad, but in my scenario no weighted views are nested inside each other. There are four of them, and some will depend on others. I am still inclined to think the warning text is incorrect and should say "dependent" not "nested". – Ollie C Apr 11 '12 at 09:38
  • @OllieC Yes, weighted views are always inside of something, but it's not always _useful_. If the parent view is 1000 pixels tall, but the children only take up 800 pixels, the weights determine how the extra space is allocated. I suspect the warning here is that the parent view is set to take up exactly the amount of space defined by its children (`wrap_content`) and so the weights are not only useless, but may trigger unneeded expensive `View` measuring during layout. I'm far from an expert, but it seems logical to me. – Argyle Apr 11 '12 at 18:10
  • You can only rarely know how many pixels are available. This does not appear to be one of those times. – AlbeyAmakiir Aug 14 '12 at 03:33
  • @AlbeyAmakiir Well, yeah. That's kind of my point. It's easier to illustrate the concept with some hard numbers, though. – Argyle Aug 14 '12 at 06:18
0

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.

Hovo
  • 790
  • 4
  • 21