RelativeLayout:基线对齐有什么问题?

时间:2013-02-06 16:26:42

标签: android android-layout

我想创建一个如下所示的布局:

The desired layout

(虚线是文本视图的基线。)

所以主锚是大 W ,然后 abc 基线对齐 W ,然后 def 高于 abc

但是当我为 abc 设置基线对齐时, def 会掉下来并变得不可见。 abc 没有问题,它完全对齐。

我找到了使 def 可见的唯一方法:它正在改变 abc 的对齐方式,因此它将与 W <的底部对齐/ em>,但这看起来很糟糕,因为较大的字形具有较大的底部边距(基线和视图底边之间的垂直空间更大)。

看起来基线对齐以某种特殊方式处理,因此布局无法计算相关视图的正确位置。

我做错了吗?我的布局是否可能?

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/w"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="W"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="100sp" />

    <TextView
        android:id="@+id/abc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/w"
        android:layout_alignParentLeft="true"
        android:text="abc"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/abc"
        android:layout_alignParentLeft="true"
        android:text="def"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

这里有一个合理但不完美的解决方法: https://groups.google.com/d/msg/android-developers/M9THu9V08vo/EouuJZv1dTAJ

  

然而,这让我发现了一个小怪癖,如果你使用layout_alignBottom和layout_alignBaseline标志将B对齐A,那么C的定位现在可以正常工作,它很高兴地位于B之上,就像使用layout_alignBottom一样,尽管B是仍与A的基线保持一致。需要注意的是,C的定位低于正常水平;它表现得好像B与A的底部对齐,然后C被定位,然后B被重新对准到A的基线,没有C改变位置。但是我可以通过一些边缘偏移来解决这个问题,至少可以通过XML完成。