Android 相对布局中心水平与另一个视图

Android relative layout center horizontally with another view

我想做这样的布局。

这些观点代表:

一些条件是: - A 应在 B 上方并与 B 水平居中 - A 的宽度总是大于 B - C 的高度总是小于 B - B和C之间不应该有差距

如果我将所有三个对象放在一个容器中,我找不到一种方法使 A 和 B 水平居中。 (layout_below 将 B 放在 A 下但左对齐)

我也试过做一个包含A和B的容器,但是如果是这样,我不能把C放在B的后面(因为A的宽度比B大,导致B和C之间有空隙)。

有什么方法可以仅使用 XML 布局来存档吗?

我认为这种布局可能适合您的需要。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/view_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/view_1"
        android:gravity="center">

        <View
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="@android:color/holo_blue_dark"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/view_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/view_1"
        android:layout_alignRight="@id/view_1"
        android:layout_below="@id/view_1"
        android:gravity="center">

        <View
            android:layout_width="50dp"
            android:layout_height="100dp"
            android:background="@android:color/holo_green_dark"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/view_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/view_2"
        android:layout_alignTop="@id/view_2"
        android:layout_toRightOf="@id/view_2"
        android:gravity="center">

        <View
            android:layout_width="75dp"
            android:layout_height="50dp"
            android:background="@android:color/holo_red_dark"/>

    </LinearLayout>

</RelativeLayout>

符合您的需求:

A: A message box that should be above B & Center horizontally with B

B: Profile picture

C: Description that should be on the right side of A & Center vertically with B

但是基于一些假设:

  1. A宽度大于B宽度。
  2. B身高大于C身高

我个人认为,如果没有您的额外代码,是不可能达到您想要的效果的。

我可以通过在视图 B 中设置 android:layout_marginLeft 来达到您想要的效果。要使视图以 A 为中心,您必须始终通过以下公式设置边距:

android:layout_marginLeft = ( (width_from_A / 4) + (width_from_B / 2) )

在我下面的示例中,width_from_A == 200dpwidth_from_B == 50dp

所以,

android:layout_marginLeft = ( (200 / 4) + (50 / 2) )
android:layout_marginLeft = ( 50 + 25 )
android:layout_marginLeft = 75dp

Java

如果您的视图并不总是具有相同的宽度,您可以通过 Java 代码动态设置边距。这应该可行,因为在您的 activity 中,就在向用户显示布局之前,您可以从 A 获取宽度,从 B 获取宽度,并手动设置 B 的 LeftMarging。

@Override
protected void onResume() {

    int widthA = findViewById(R.id.viewA).getLayoutParams().width;
    int widthB = findViewById(R.id.viewB).getLayoutParams().width;

    // THIS LINE JUST WORK FOR API>17
    ((RelativeLayout.LayoutParams)findViewById(R.id.viewB).getLayoutParams()).setMarginStart(widthA/4 + widthB/2);

    //FOR ANY API
    ((RelativeLayout.LayoutParams)findViewById(R.id.viewB).getLayoutParams()).setMargins(widthA/4 + widthB/2,0,0,0);

    super.onResume();
}

布局

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

    <TextView
        android:id="@+id/viewA"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="A"
        android:background="@android:color/holo_blue_dark"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/viewA">

        <TextView
            android:id="@+id/viewB"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="B"
            android:layout_marginLeft="75dp"
            android:background="@android:color/holo_green_dark"/>


        <TextView
            android:id="@+id/viewC"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="C"
            android:layout_toRightOf="@+id/viewB"
            android:background="@android:color/holo_red_dark"/>
    </RelativeLayout>
</RelativeLayout>

结果