问题描述
如何删除文本顶部和底部的间距?我想使两个文本彼此接触或至少减小两个文本之间的间距。
我看过其他推荐设置android:includeFontPadding="false"
的帖子,但这不起作用。
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="96sp"
android:text="TEXT1"
android:background="@color/primaryColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="96sp"
android:text="TEXT2"
android:background="@color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
解决方法
问题在于,不仅是空白空间,还包括字体的上升和下降,例如 g 的下部。 TextView
必须具有该空间,因为它不知道您不会使用它。 includeFontPadding
会影响上升沿上的某些重音,因此您可以删除一些空间,但其余空间是必需的。
要获得没有上升和下降关系的字体,我认为您最好的做法是将下部TextView
的顶部限制为上部{strong>顶部,并添加一个marginTop
值,将其向下移动到所需位置。使用一个 sp 值而不是 dp ,它相对于用户的字体大小首选项保持不变-如果更改字体,则需要对其进行调整。如果您使用的是背景,则背景必须在文本视图后面分开
如果您绝对愿意为此付出任何代价,那么您可以执行以下操作,我尚未检查它是否可以在不同的屏幕尺寸下使用。但是您可以将它们的顶部都附加到同一视图上,可以是父级,然后使背景透明,如果您选择了不同的颜色,那么这将行不通,请将文本全部大写,以使常规字母不能再高一些或更低。根据要在dp中提供的文本大小来保持边距。这可能是“我希望以任何方式做到这一点”的方法,而不是可接受的方法。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="95dp"
android:text="TEXT1"
android:background="@android:color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="0dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="TEXT2"
android:textSize="95dp"
android:layout_marginTop="75dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
,
您可以使用Space
视图来做一些聪明的事情来使它起作用:
<TextView android:id="@+id/textView1"
[other TextView stuff]
app:layout_constraintTop_toTopOf="parent"/>
<Space android:id="@+id/space"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="@id/textView1"/>
<TextView android:id="@+id/textView2"
[other TextView stuff]
app:layout_constraintTop_toBottomOf="@id/space"/>
然后在Space
视图中使用margin参数进行拨弄,直到找到所需的文本为止。