2

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

想要的布局

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

所以主锚是大的W,然后abc与W基线对齐,然后def在abc之上。

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

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

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

我做错了吗?我的布局有可能吗?

这是 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>
4

1 回答 1

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 完成。

于 2013-10-31T21:49:30.037 回答