" LinearLayout或其RelativeLayout父级是无用的"警告

时间:2015-06-27 22:58:54

标签: android android-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>
...

并且我不明白为什么会收到警告&#34;此LinearLayout布局或其RelativeLayout父布局可能无用&#34;。

我尝试使用我的布局设计是将sublayaout1和sublayaout2置于屏幕中间。为了做到这一点,我需要android:layout_centerInParent属性为true,我需要RelativeLayout父类作为容器,因为它在一个有多行的水平LinearLayout内。

在没有RelativeLayout的情况下,有没有办法让LinearLayout居中? 谢谢

1 个答案:

答案 0 :(得分:1)

android:gravity="center"上使用LinearLayout。删除不必要的RelativeLayout。不要与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>