【问题标题】:How to align view relative to multiple views in RelativeLayout?如何相对于RelativeLayout中的多个视图对齐视图?
【发布时间】:2018-06-06 04:59:02
【问题描述】:

我有一个父 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 能解决这个问题吗?

【问题讨论】:

    标签: android android-layout android-constraintlayout android-relativelayout


    【解决方案1】:

    试试这个:

    <?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"

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

    因此,如果视图 1 可见性消失,则视图 3 将保持在视图 2 之上。

    您可以通过将可见性设置为“查看一号”来测试这一点。

    ConstraintLayout 能解决这个问题吗?

    是的。

    【讨论】:

      【解决方案2】:

      我们可以通过编程实现这一点。

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

      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 保持在视图 1 的顶部,并且由于高度为零,视图 3 将出现在视图 2 的顶部。

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

        【讨论】: