Android - 在同一基线上对齐不同的字体大小
Android - Align different font-sizes on same baseline
我有 2 个相邻的 TextView
,每个都有不同的字符串,字体大小也不同。我希望文本在每个 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>
这是当前的行为,注意 2 个 TextView
有不同的基线("hello" 低于 30),因为每个 TextView
的字体是不同的尺寸。
将两个 TextView
元素的高度更改为 wrap_content
。默认情况下,水平 LinearLayout
将自动对齐 TextView 子项的基线。
如果您当前使用 match_parent
作为高度以获得全高背景色,则您必须想出不同的方法来实现。也许,您可以在 LinearLayout
上使用背景颜色,然后仅在较大的 TextView
上指定背景颜色;这将产生与今天相同的效果。
在ConstraintLayout
中你可以简单地使用
app:layout_constraintBaseline_toBaselineOf="@+id/textView1"
我有 2 个相邻的 TextView
,每个都有不同的字符串,字体大小也不同。我希望文本在每个 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>
这是当前的行为,注意 2 个 TextView
有不同的基线("hello" 低于 30),因为每个 TextView
的字体是不同的尺寸。
将两个 TextView
元素的高度更改为 wrap_content
。默认情况下,水平 LinearLayout
将自动对齐 TextView 子项的基线。
如果您当前使用 match_parent
作为高度以获得全高背景色,则您必须想出不同的方法来实现。也许,您可以在 LinearLayout
上使用背景颜色,然后仅在较大的 TextView
上指定背景颜色;这将产生与今天相同的效果。
在ConstraintLayout
中你可以简单地使用
app:layout_constraintBaseline_toBaselineOf="@+id/textView1"