android – 在RelativeLayout中设置minHeight,禁用alignBaseline?

我的RelativeLayout看起来应该更像这样:

+----------------------------------------------+
|                                              |
|                                              |
|+----------------+          +----------------+|
||                |+--------+|                ||
||     View 1     || View 2 ||     View 3     ||
|+----------------++--------++----------------+|
|                                              |
|                                              |
+----------------------------------------------+

当我的相对布局具有固定高度(例如80dp)时,我完美地实现了它.不幸的是我需要它有android:layout_height =“wrap_content”与android:minHeight设置(例如到那些80dp).当我以这种方式更改我的布局时,android:layout_alignBaseline似乎不适用于View2,我得到类似的东西:

+----------------------------------------------+
|                                              |
|                  +--------+                  |
|+----------------+| View 2 |+----------------+|
||                |+--------+|                ||
||     View 1     |          |     View 3     ||
|+----------------+          +----------------+|
|                                              |
|                                              |
+----------------------------------------------+

我的xml看起来像这样:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="80dp" >

    <TextView
        android:id="@+id/View1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/View2"
        android:gravity="center"
        android:text="View1" />

    <TextView
        android:id="@+id/View2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/View1"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="View2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/View2"
        android:gravity="center"
        android:text="View3" />

</RelativeLayout>

任何人都知道为什么它不起作用?

最佳答案 经过几次测试后,我得出的结论是,在RelativeLayout中设置height到wrap_content只会被窃听.它不能与不填充布局整个高度的视图正确配合.对于这种视图,RelativeLayout存在垂直居中的问题(规则
CENTER_VERTICAL
CENTER IN PARENT).基本上,应该垂直居中的视图将像它们一样显示,但布局本身将“看到”它们(为了设置其他规则)作为对齐顶部.

因此,将任何规则设置为依赖于那些“错误地通过布局感知”视图的位置,将导致以非预期方式显示的视图.

如果考虑到这一点,我的案例很容易解释.这是视图的显示方式:

+----------------------------------------------+
|                                              |
|                  +--------+                  |
|+----------------+| View 2 |+----------------+|
||                |+--------+|                ||
||     View 1     |          |     View 3     ||
|+----------------+          +----------------+|
|                                              |
|                                              |
+----------------------------------------------+

这就是RelativeLayout“感知”它们的方式:

+----------------------------------------------+
|+----------------+          +----------------+|
||                |+--------+|                ||
||     View 1     || View 2 ||     View 3     ||
|+----------------++--------++----------------+|
|                                              |
|                                              |
|                                              |
|                                              |
+----------------------------------------------+

(两个图片中的视图2位置相同).

由于我需要将我的布局设置为RelativeLayout,为了解决这个问题,我决定完全放弃使用wrap_content值,并自己控制视图的高度.它使我的布局复杂一点,但只要我控制它,它就好了.

点赞