【问题标题】:Android relative layout center horizontally with another viewAndroid相对布局中心水平与另一个视图
【发布时间】:2016-11-25 03:01:31
【问题描述】:

我想做一些这样的布局。

这些观点代表:

  • A 是一个消息框。
  • B 是头像
  • C 是一个描述,应该在 A 的右侧 & 与 B 垂直居中

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

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

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

有什么方法可以只用 XML 布局存档?

【问题讨论】:

  • A 和 B 有不同的尺寸(宽度)吗?
  • A 总是大于 B

标签: android xml layout views center


【解决方案1】:

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

我可以通过在视图 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 代码动态设置边距。这应该可以工作,因为在您的活动中,就在向用户显示布局之前,您可以从 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>

结果

【讨论】:

  • 感谢您的回答!除了我使用A.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);layoutParams.leftMargin = A.getMeasuredWidth() / 2 + B.getMeasuredWidth() / 2 之外,我最终也以同样的方式实现了我的。
【解决方案2】:

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

<?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:一个消息框,应该在 B 上方并与 B 水平居中

B:头像

C:描述应该在A的右侧&与B垂直居中

但是基于一些假设:

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

【讨论】:

  • 我试过了,但是 C 必须紧跟在 A 之后。在这种布局中,A 和 C 之间存在间隙,取决于 A 的宽度。我将编辑我的问题以澄清这一点。不过谢谢你的回答。
猜你喜欢
  • 2019-04-10
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
相关资源
最近更新 更多