243

当我在文本中有 aTextView和 a\n时,在右边我有两个singleLine TextViews,一个在另一个下方,中间没有空格。我为所有三个TextViews 设置了以下内容。

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

左边的第一行TextView与右上角完美对齐TextView

左边第二行TextView略高于右下第二行TextView

s的顶部和底部似乎有某种隐藏的填充TextView。我怎样才能删除它?

4

20 回答 20

586
setIncludeFontPadding (boolean includepad)

或者在XML这将是:

android:includeFontPadding="false"

设置是否TextView包括额外的顶部和底部填充,以便为超出正常上升和下降的口音腾出空间。默认值为真。

于 2011-03-03T09:48:55.580 回答
44

我搜索了很多正确的答案,但没有在哪里可以找到可以完全删除所有填充的答案TextView,但最终在通过官方文档后解决了单行文本

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

将这两行添加到TextViewxml 将完成工作。
第一个属性删除为重音保留的填充,第二个属性删除保留的间距以保持两行文本之间的适当间距。

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

于 2016-10-19T09:36:46.127 回答
42

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

我的解决方案? layout_marginBottom="-3dp"TextView给你一个解决方案,BAM!

虽然,-3dplayout_marginTop失败....呃。

于 2011-09-23T05:57:04.107 回答
26

您可以尝试使用此属性(ConstraintLayout):layout_constraintBaseline_toBaselineOf

像这样:

应用程序:layout_constraintBaseline_toBaselineOf="@+id/textView"

在此处输入图像描述

在此处输入图像描述

于 2018-10-31T09:08:02.173 回答
20

更新的 XML

android:fontFamily="monospace"
android:includeFontPadding="false"
于 2020-05-19T03:44:57.270 回答
19

如果您使用AppCompatTextView(或从前API 28),您可以使用这两个属性的组合来删除第一行的间距:

XML

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

科特林

text.firstBaselineToTopHeight = 0
text.includeFontPadding = false
于 2019-06-04T14:13:21.687 回答
13

这也让我很恼火,我发现的答案是字体本身实际上有额外的空间,而不是 TextView。这是相当恼人的,来自文档发布背景,您对 Android 对排版元素的控制量有限。我建议使用可能没有此问题的自定义字体(例如 Bitstream Vera Sans,它已获得再分发许可)。不过,我不确定它是否确实如此。

于 2011-01-22T17:01:54.663 回答
13

我删除了自定义视图中的间距——NoPaddingTextView。

https://github.com/SenhLinsh/NoPaddingTextView

在此处输入图像描述

package com.linsh.nopaddingtextview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.TextView;

/**
 * Created by Senh Linsh on 17/3/27.
 */

public class NoPaddingTextView extends TextView {

    private int mAdditionalPadding;

    public NoPaddingTextView(Context context) {
        super(context);
        init();
    }


    public NoPaddingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setIncludeFontPadding(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int yOff = -mAdditionalPadding / 6;
        canvas.translate(0, yOff);
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        getAdditionalPadding();

        int mode = MeasureSpec.getMode(heightMeasureSpec);
        if (mode != MeasureSpec.EXACTLY) {
            int measureHeight = measureHeight(getText().toString(), widthMeasureSpec);

            int height = measureHeight - mAdditionalPadding;
            height += getPaddingTop() + getPaddingBottom();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int measureHeight(String text, int widthMeasureSpec) {
        float textSize = getTextSize();

        TextView textView = new TextView(getContext());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setText(text);
        textView.measure(widthMeasureSpec, 0);
        return textView.getMeasuredHeight();
    }

    private int getAdditionalPadding() {
        float textSize = getTextSize();

        TextView textView = new TextView(getContext());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setLines(1);
        textView.measure(0, 0);
        int measuredHeight = textView.getMeasuredHeight();
        if (measuredHeight - textSize > 0) {
            mAdditionalPadding = (int) (measuredHeight - textSize);
            Log.v("NoPaddingTextView", "onMeasure: height=" + measuredHeight + " textSize=" + textSize + " mAdditionalPadding=" + mAdditionalPadding);
        }
        return mAdditionalPadding;
    }
}
于 2017-03-03T03:01:10.540 回答
12

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

于 2020-07-17T10:23:18.503 回答
4

由于我的要求是覆盖现有的 textView get from findViewById(getResources().getIdentifier("xxx", "id", "android"));,所以我不能简单地尝试onDraw()其他答案。

但我只是想出了解决问题的正确步骤,这是 Layout Inspector 的最终结果:

在此处输入图像描述

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

这是修复它的关键代码:

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);

在此处输入图像描述

没有setPadding(0, 0, 0, 0);

在此处输入图像描述

没有setIncludeFontPadding(false);

在此处输入图像描述

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

在此处输入图像描述

于 2017-11-02T14:05:12.210 回答
4
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/baselineImage"
        android:includeFontPadding="false" />

    <ImageView
        android:id="@+id/baselineImage"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:baselineAlignBottom="true"
        android:layout_alignParentBottom="true" />

    <!-- This view will be exactly 10dp below the baseline of textView -->
    <View
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/baselineImage" />

</RelativeLayout>

使用额外的 ImageView,我们可以将 TextView 设置为与 ImageView 对齐的基线,并将 ImageViewandroid:baselineAlignBottom上的设置为 true,这将使 ImageView 的基线位于底部。其他视图可以使用 ImageView 的底部对齐自身,该底部本身与 TextView 的基线相同。

然而,这只修复了填充底部而不是顶部。

于 2016-01-27T06:36:28.540 回答
3

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

 <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" />

这些是结果:

截图-1

截图 3

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

于 2017-03-06T03:31:12.800 回答
1

唯一有效的是

android:lineSpacingExtra="-8dp"
于 2018-09-20T06:27:33.637 回答
1

简单的方法有效:

setSingleLine();
setIncludeFontPadding(false);

如果它不起作用,请尝试在该代码上方添加此代码:

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

如果您需要多行,也许您需要通过临时单行 TextView(删除填充之前和之后)精确计算填充顶部和底部的高度,然后使用负填充或一些带有平移 Y 的幽灵布局应用降低高度结果。哈哈

于 2018-09-02T03:57:01.877 回答
0

To my knowledge this is inherent to most widgets and the amount of "padding" differs among phone manufacturers. This padding is really white space between the image border and the image in the 9 patch image file.

For example on my Droid X, spinner widgets get extra white space than buttons, which makes it look awkward when you have a spinner inline with a button, yet on my wife's phone the same application doesn't have the same problem and looks great!

The only suggestion I would have is to create your own 9 patch files and use them in your application.

Ahhh the pains that are Android.

Edited: Clarify padding vs white space.

于 2011-01-22T15:58:37.183 回答
0

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

于 2011-01-22T15:44:16.100 回答
0

这个技巧对我有用(对于min-sdk >= 18)。

我使用android:includeFontPadding="false"负边距,例如android:layout_marginTop="-11dp"并将我TextView的放入FrameLayout或任何 ViewGroup...

在此处输入图像描述

最后是示例代码:

<LinearLayout
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    >

    <TextView
        style="@style/MyTextViews.Bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        android:textSize="48sp"
        android:layout_marginTop="-11dp"
        android:includeFontPadding="false"
        tools:text="1"/>
</LinearLayout>
于 2020-04-20T20:01:20.700 回答
0

使用约束布局作为你的根视图,然后添加一个指南助手。

例子:

<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 值只是示例,它取决于您的需要。

于 2021-08-11T09:46:24.550 回答
-1

看到这个:

将 ImageView 与 EditText 水平对齐

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

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

android:background="@drawable/myEditBackground"
于 2012-06-18T09:54:34.247 回答
-8

在您的 ImageView xml 中使用它

机器人:adjustViewBounds =“真”

于 2018-12-17T06:17:37.320 回答