如何相对于 RelativeLayout 中的多个视图对齐视图?

How to align view relative to multiple views in RelativeLayout?

我有一个父 RelativeLayout 和 3 个子视图。视图一和视图二对齐父底部。如果视图一可见,我希望我的视图三在视图一上方对齐,否则如果视图一不可见,则在视图二上方对齐。

这是我的布局:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <View
        android:id="@+id/one"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:background="@color/green"
        tools:visibility="visible"/>

    <View
        android:id="@+id/two"
        android:layout_width="250dp"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/red"/>

    <View
        android:id="@+id/three"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@id/one"
        android:layout_marginBottom="10dp"
        android:background="@color/yellow"/>

</RelativeLayout>

我知道如果我将视图 1 和视图 2 放入另一层并将视图 3 对齐到另一层上方,它会起作用,但对于当前的体系结构,我不允许这样做。还有其他方法可以实现这一目标吗? ConstraintLayout 能解决这个问题吗?

试试这个:

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <View
        android:id="@+id/one"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_above="@+id/two"
        android:background="@android:color/holo_green_dark"
        tools:visibility="visible"/>

    <View
        android:id="@+id/two"
        android:layout_width="250dp"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/holo_red_dark"/>

    <View
        android:id="@+id/three"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/one"
        android:layout_marginBottom="10dp"
        android:background="@android:color/holo_blue_bright"/>

</RelativeLayout>

问题是你只需添加

android:layout_above="@+id/two"

查看一个使其停留在视图二之上并添加

android:layout_above="@+id/one"

查看三,这将使它保持在视图一之上。

因此,如果视图一的可见性消失,视图三将保持在视图二之上。

您可以通过将可见性设置为消失来测试视图一。

Would ConstraintLayout be able to solve this issue?

是的。

我们可以通过编程来实现。

找到下面的代码,根据第一个视图的可见性状态对齐第三个视图。

final View view3 = findViewById(R.id.three);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.addRule(RelativeLayout.ABOVE, view3.getVisibility() == View.VISIBLE ? R.id.one : R.id.two);
view3.setLayoutParams(params);

如果您想使用当前的布局,则可以通过编程将视图一的高度设置为零,而不是更改其可见性。在这种情况下,视图 3 仍位于视图 1 的顶部,并且由于高度为零,视图 3 将出现在视图 2 的顶部。

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.one);
relativeLayout.getLayoutParams().height = 0;