如何删除Android的textview的顶部和底部空间

当我将下面的XML包含到布局文件中时,我可以看到下面的图像。 如果你看到它,你可以意识到TextView有顶部和底部的空间。

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="E1" android:background="#ff00ff00"/> 

在这里输入图像描述

我想删除这个空间。 如何删除它? 这叫什么? 如果有人有线索,请让我知道。 提前致谢。

尝试android:includeFontPadding="false"来查看是否有帮助。 根据我的经验,这将有一点帮助,但是没有办法将TextView尺寸缩小到完全像素完美的文字尺寸。

唯一的select,可能会或可能不会给出更好的结果,是作弊和硬连线尺寸来匹配文本大小,例如"24sp"而不是"wrap_content"的高度。

尝试将底部和顶部边距设置为负数。

像这样的东西:

 android:layout_marginTop="-5dp" android:layout_marginBottom="-5dp" 

调整相应的值。

我有同样的问题。 属性android:includeFontPadding="false"不适用于我。 我已经用这种方法解决了这个问题:

 public class TextViewWithoutPaddings extends TextView { private final Paint mPaint = new Paint(); private final Rect mBounds = new Rect(); public TextViewWithoutPaddings(Context context) { super(context); } public TextViewWithoutPaddings(Context context, AttributeSet attrs) { super(context, attrs); } public TextViewWithoutPaddings(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(@NonNull Canvas canvas) { final String text = calculateTextParams(); final int left = mBounds.left; final int bottom = mBounds.bottom; mBounds.offset(-mBounds.left, -mBounds.top); mPaint.setAntiAlias(true); mPaint.setColor(getCurrentTextColor()); canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); calculateTextParams(); setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 1); } private String calculateTextParams() { final String text = getText().toString(); final int textLength = text.length(); mPaint.setTextSize(getTextSize()); mPaint.getTextBounds(text, 0, textLength, mBounds); if (textLength == 0) { mBounds.right = mBounds.left; } return text; } } 

我面临同样的问题。 下面是一个很好的答案: 如何将文本alignment到TextView的顶部?

但是代码还没有完成,不支持所有的字体大小。 改变线

 int additionalPadding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics()); 

 int additionalPadding = getTextSize() - getLineHeight(); 

完整的C#代码(单声道)删除顶部偏移量:

 public class TextControl : TextView { public TextControl (Context context) : base (context) { SetIncludeFontPadding (false); Gravity = GravityFlags.Top; } protected override void OnDraw (Android.Graphics.Canvas canvas) { if (base.Layout == null) return; Paint.Color = new Android.Graphics.Color (CurrentTextColor); Paint.DrawableState = GetDrawableState (); canvas.Save (); var offset = TextSize - LineHeight; canvas.Translate (0, offset); base.Layout.Draw (canvas); canvas.Restore (); } } 

只是想添加到DynamicMind的答案 ,为什么你看到的TextViews周围的空间是填充9补丁默认使用的背景

9-patch技术可以让你指定一个内容区域 ,实际上是填充。 除非您明确设置视图的填充,否则将使用该填充。 例如,当以编程方式将9补丁背景设置为设置了填充的视图时,它们将被覆盖。 反之亦然,如果你设置了填充,他们将重写9-patch背景设置的内容。

不幸的是,在XML布局中,不可能确定这些操作的顺序。 我认为只是从您的TextView中删除背景将有助于:

 android:background="@null" 
 public class TopAlignedTextView extends TextView { public TopAlignedTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TopAlignedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs); setIncludeFontPadding(false); //remove the font padding setGravity(getGravity() | Gravity.TOP); } @Override protected void onDraw(Canvas canvas) { TextPaint textPaint = getPaint(); textPaint.setColor(getCurrentTextColor()); textPaint.drawableState = getDrawableState(); canvas.save(); //remove extra font padding int yOffset = getHeight() - getBaseline(); canvas.translate(0, - yOffset / 2); if (getLayout() != null) { getLayout().draw(canvas); } canvas.restore(); } } 

这是节省了我们一天的代码。 它是从maksimko的单C#代码改编的:

 public class TopAlignedTextView extends TextView { public TopAlignedTextView(Context context) { super(context); } /*This is where the magic happens*/ @Override protected void onDraw(Canvas canvas){ float offset = getTextSize() - getLineHeight(); canvas.translate(0, offset); super.onDraw(canvas); } } 

我仍然不得不围绕textView.setIncludeFontPadding(false)进行游戏,因为我们使用不同的字体大小alignmentTextViews

你有没有定义一个布局保证金? 例如:

 android:layout_marginTop="5dp" 

否则,如果你的文本视图被包裹在一个LinearLayout或其他容器中,那么这个感觉就会有填充或边距。

 android:background="@android:drawable/editbox_background" 

根据你的使用它改变你想要的editbox_background。 因为android提供了一些像上面代码一样的编译select根据您的要求。 可能是对你充满帮助。

在LinearLayout中,默认的填充可能是一个问题。 尝试将其设置为0dp。 它为我工作。

TopAlignedTextView代码的答案 : TopAlignedTextView @ GitHub

使用它的布局:

 <com.github.captain_miao.view.TopAlignedTextView android:id="@+id/text_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" android:text="@string/text_demo_a" /> 

在这里输入图像描述

我用这种方法解决这个问题非常不方便,但是我设法让文本坐在我想要的位置,把文本视图的高度设置为静态的,并且一直摆弄它,直到它几乎不适合文本。 在我的情况下,我使用的字体样式有64sp的高度,所以我把我的textview的高度设置为50sp,它工作正常。 我也必须将foreground_gravity设置为底部。