Android - 在同一基线上对齐不同的字体大小

Roy*_*son 4 android textview android-layout android-xml

我有 2 个相邻的TextViews,每个都有一个不同的字符串,具有不同的字体大小。我希望文本在每个TextView. 我怎样才能做到这一点?

这是我的布局:

   <LinearLayout
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:background="@color/colorAccentLight"
            android:textSize="18sp"
            style="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:textAlignment="gravity"
            android:text="30"
        />
        <TextView
            android:background="@color/colorAccent"
            android:text="hello"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:textAlignment="gravity"
        />

    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是当前的行为,请注意 2 TextViews如何具有不同的基线(“hello”低于 30),因为每个的字体TextView大小不同。

在此处输入图片说明

Fra*_*cis 8

ConstraintLayout你可以简单地使用

app:layout_constraintBaseline_toBaselineOf="@+id/textView1"
Run Code Online (Sandbox Code Playgroud)


Ben*_* P. 2

TextView将两个元素的高度更改为wrap_content为。默认情况下,水平线LinearLayout会自动对齐 TextView 子级的基线。

如果您当前使用match_parentheight 来获得全高背景颜色,则必须考虑一种不同的方法来实现这一点。也许,您可以在 上使用背景颜色LinearLayout,然后仅在较大的 上指定背景颜色TextView;这将产生与今天相同的效果。