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

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

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

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

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


当前回答

简单的方法奏效:

setSingleLine();
setIncludeFontPadding(false);

如果它没有工作,然后尝试添加这个代码:

setLineSpacing(0f,0f);
// and set padding and margin to 0

如果你需要多行,也许你需要通过临时单行TextView精确计算填充顶部和底部的高度(在删除填充之前和之后),然后应用降低高度结果与负填充或一些鬼布局翻译Y. Lol

其他回答

这招对我很管用:

android:minHeight="0dp"

你可以尝试使用这个属性(ConstraintLayout):layout_constraintBaseline_toBaselineOf

是这样的:

app: layout_constraintBaseline_toBaselineOf = @ + id / textView”

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

 <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中的第二行高一些,但它们的基线仍然是对齐的。

我能感受到你的痛苦。我已经尝试了上面的每个答案,包括setIncludeFontPadding为false,这对我没有任何帮助。

我的解决方案吗?layout_marginBottom="-3dp"在TextView给你一个解决方案的底部, 砰!

虽然,-3dp在layout_marginTop失败....ugh。

因为我的要求是覆盖现有的textView从findViewById(getResources()。getIdentifier(“xxx”,“id”,“android”);,所以我不能简单地尝试其他答案的onDraw()。

但我只是想出了正确的步骤来解决我的问题,这是布局检查器的最终结果:

因为我想要的只是删除顶部的空格,所以我不需要选择其他字体来删除底部的空格。

以下是修复它的关键代码:

Typeface mfont = Typeface.createFromAsset(getResources().getAssets(), "fonts/myCustomFont.otf");
myTextView.setTypeface(mfont);

myTextView.setPadding(0, 0, 0, 0);

myTextView.setIncludeFontPadding(false);

第一个键是设置自定义字体“fonts/myCustomFont”。otf”有底部的空间,但不是在顶部,你可以很容易地找到这通过打开otf文件,并点击android Studio中的任何字体:

正如你所看到的,底部的光标有额外的间距,但顶部没有,所以它解决了我的问题。

第二个关键是您不能简单地跳过任何代码,否则它可能无法工作。这就是为什么你会发现一些人评论说某个答案有效,而另一些人则评论说它无效。

让我们来说明如果我移除其中一个会发生什么。

没有setTypeface (mfont);:

没有整理(0,0,0,0);

没有setIncludeFontPadding(虚假);:

没有其中3个(即原件):