0

I want a SearchView at the top of an Activity's layout, and then below that I need to add two fragments aligned horizontally next to each other, such that the right one take double the horizontal space than the left one.

So what I mean is:

The ViewGroup containing both the fragments should take up all the remaining space after the SearchView is drawn. So I gave it a layout_weight of 1. Then I added a LinearLayout with two FrameLayouts for the fragmnets, and gave the left one a Layout_weight of 1, and the right one the value of 2.

But I get the Lint warning that nested weights are bad for performance.

I read about it on SO and found this:

Layout weights require a widget to be measured twice.

This basic tutorial on the developer training here says this thing this way:

To improve the layout efficiency when you specify the weight, you should change the width of the EditText to be zero (0dp). Setting the width to zero improves layout performance because using "wrap_content" as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

But since in my layout I am setting the layout_width="0dp" when the parent LinearLayout's orientation is vertical, and layout_height="0dp" when the parent LinearLayout's orientation is horizontal. Why am I still getting the Lint warning?

What can I do about this? Because RelativeLayout can't specify how much of the "remaining space" is an element going to take (specified by layout_weight, which does not seem to be included in RelativeLayout.LayoutParams).

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="${relativePackage}.${activityClass}" >

    <android.support.v7.widget.SearchView
        android:id="@+id/searchActivity_searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="match_parent" >
        <FrameLayout 
            android:id="@+id/searchActivity_frameLayout"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" /><!-- Nested weights are bad for performance Lint Warning -->

        <FrameLayout
            android:id="@+id/searchActivity_frameLayout1"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>



</LinearLayout>
Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183

1 Answers1

1

It's a warning, in this case you should be OK, it's more to keep you from abusing a lot of nested weights and bogging down your app. The other option is to get the screen size at run time and set the width of the Frame Layouts manually.

Dave S
  • 3,378
  • 1
  • 20
  • 34