绝对视图位置重力保持与父项的父项而不是父项

时间:2016-03-06 15:50:13

标签: android layout-gravity

我可以在黑色布局的中心设置蓝色视图了解黑色布局是否是父母的父母

就像在图像中一样

center parent of parent

enter image description here

1 个答案:

答案 0 :(得分:0)

你必须稍微改变一下,不能把视图放在父母的父母身上。

您需要将RelativeLayout作为根布局,然后将蓝线放在垂直中心。然后,您将红色LinearLayouts放置在蓝色布局的上方和下方。像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/blue_layout"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_centerVertical="true"
        android:background="#ff0000ff" />

    <LinearLayout
        android:id="@+id/above"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/blue_layout"
        android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/below"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/blue_layout"
        android:orientation="vertical">

    </LinearLayout>

</RelativeLayout>
相关问题