Fran
Fran

Reputation: 636

"LinearLayout or its RelativeLayout parent is useless" warning

I have the following layout

...
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".8">
        <LinearLayout
            android:layout_width="320dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_centerInParent="true"
            android:baselineAligned="false">
            <include layout="@layout/sublayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center_vertical" />
            <include layout="@layout/sublayout2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
</RelativeLayout>
...

and I don't understand why I get the warning "This LinearLayout layout or its RelativeLayout parent is possibly useless".

What I'm trying to do with my layout design is to center both sublayaout1 and sublayaout2 in the middle of the screen. In order to do that I need the android:layout_centerInParent attribute to be true and I need the RelativeLayout parent as a container since it is inside an horizontal LinearLayout with multiple rows.

Is there any way to center the the LinearLayout without the RelativeLayout? Thank you

Upvotes: 2

Views: 671

Answers (1)

Eugen Pechanec
Eugen Pechanec

Reputation: 38233

Use android:gravity="center" on the LinearLayout. Remove the unnecessary RelativeLayout. Not to be confused with android:layout_gravity.

<LinearLayout
    android:layout_width="320dp"
    android:layout_height="0dp"
    android:layout_weight=".8"
    android:orientation="horizontal"
    android:gravity="center"
    android:baselineAligned="false">
    <include layout="@layout/sublayout1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center_vertical" />
    <include layout="@layout/sublayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />
</LinearLayout>

Upvotes: 1

Related Questions