绝对视图位置重力留在父级而不是父级

absolute view position gravity stay with parent of parent instead of parent

我可以将蓝色视图设置在黑色布局的中心知道黑色布局是它的父级吗

喜欢图片

center parent of parent

你必须稍微改变一下,你不能相对于 parent 的 parent 放置视图。

您需要一个 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>