代码之家  ›  专栏  ›  技术社区  ›  HeyThere

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

  •  1
  • HeyThere  · 技术社区  · 7 年前

    我有一个家长亲戚和三个孩子的看法。视图一和视图二是对齐父底部。如果视图一可见,我希望视图三在视图一上方对齐,否则,如果视图一不可见,请在视图二上方对齐。

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

    我知道,如果我将视图一和视图二放在另一个层中,并将视图三与另一个层对齐,它将起作用,但在当前架构中,我不允许这样做。有没有其他方法可以做到这一点?ConstraintLayout能够解决这个问题吗?

    3 回复  |  直到 6 年前
        1
  •  3
  •   Nongthonbam Tonthoi    7 年前

    试试这个:

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

    视图三将使其保持在视图一之上。

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

    ConstraintLayout能够解决这个问题吗?

        2
  •  0
  •   Prasad Nicholas Harlen    7 年前

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

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

    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
  •  0
  •   Muhammad Umer Farooq    7 年前

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

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