当我有一个TextView与一个\n在文本中,,在右边我有两个单线TextViews,一个在另一个之间没有间距。我已经为所有三个textview设置了以下内容。

android:lineSpacingMultiplier="1" 
android:lineSpacingExtra="0pt" 
android:paddingTop="0pt" 
android:paddingBottom="0pt"

第一行的左侧TextView线完美地与右上角TextView。

左TextView的第二行略高于右下TextView的第二行。

似乎有某种隐藏的填充顶部和底部的TextViews。我怎么才能去掉它呢?


当前回答

我搜索了很多正确的答案,但没有在哪里我可以找到一个答案,完全可以从TextView删除所有的填充物,但最后通过官方文档得到了一个工作围绕单行文本

android:includeFontPadding="false"
android:lineSpacingExtra="0dp"

将这两行添加到TextView xml中就可以了。 第一个属性删除为重音保留的填充,第二个属性删除为保持两行文本之间适当的空间而保留的间距。

确保不要在多行TextView中添加lineSpacingExtra="0dp",因为它可能会使外观笨拙

其他回答

看到这个:

水平对齐ImageView和EditText

似乎EditText的背景图像有一些透明的像素,这也增加了填充。

一种解决方案是将EditText的默认背景更改为其他内容(或者什么都不更改,但EditText没有背景可能是不可接受的)。这可以设置android:background XML属性。

android:background="@drawable/myEditBackground"

添加android:includeFontPadding="false",看看是否有帮助。并使文本视图大小与文本大小相同,而不是“包装内容”。它肯定会起作用。

您可能想要尝试将左侧文本视图的底部与右侧第二个文本视图的底部对齐。

使用constraintlayout作为根视图,然后添加一个指导线助手。

例子:

<TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 app:layout_constraintTop_toTopOf="parent" 
 app:layout_constraintStart_toStartOf="parent" />

<TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/guideline" />

<androidx.constraintlayout.widget.Guideline
 android:id="@+id/guideline"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 app:layout_constraintGuide_begin="20dp" />

属性layout_constraintGuide_begin值只是一个例子,它取决于你的需要。

我认为这个问题可以这样解决:

 <TextView
        android:id="@+id/leftText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Hello World!\nhello world" />

 <TextView
        android:id="@+id/rightUpperText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_alignTop="@+id/leftText"
        android:textSize="30dp"
        android:text="Hello World!" />

 <TextView
        android:id="@+id/rightLowerText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_below="@+id/rightUpperText"
        android:textSize="30dp"
        android:text="hello world" />

结果如下:

ScreenShot-1

ScreenShot-3

虽然rightLowerText中的特殊字符行看起来比leftText中的第二行高一些,但它们的基线仍然是对齐的。