Android:TextView:删除顶部和底部的间距和填充
当我有一个文本中带有 \n
的 TextView
时,右侧有两个 singleLine
TextView
s,一个在另一个下面,中间没有间距。我为所有三个 TextView 设置了以下内容。
android:lineSpacingMultiplier="1"
android:lineSpacingExtra="0pt"
android:paddingTop="0pt"
android:paddingBottom="0pt"
左侧 TextView
的第一行与右上角 TextView
完美对齐。
左侧TextView
第二行比右下TextView
第二行高一点。
似乎在 TextView
的顶部和底部有某种隐藏的填充。我怎样才能删除它?
When I have a TextView
with a \n
in the text,, on the right I have two singleLine
TextView
s, one below the other with no spacing in between. I have set the following for all three TextView
s.
android:lineSpacingMultiplier="1"
android:lineSpacingExtra="0pt"
android:paddingTop="0pt"
android:paddingBottom="0pt"
The first line of the left TextView
lines up perfectly with the top right TextView
.
The second line of the left TextView
is a little higher than the second line of the bottom right TextView
.
It seems that there is some kind of hidden padding on the top and the bottom of the TextView
s. How can I remove that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
简单的方法有效:
如果它不起作用,则尝试在代码上方添加此代码:
如果您需要多行,也许您需要通过临时单行 TextView 精确计算填充顶部和底部的高度(在删除填充之前和之后) ) ,然后使用负填充或一些带有平移 Y 的幽灵布局应用降低高度结果。哈哈
Simple method worked:
If it not worked, then try to add this above that code:
If you need multi line, maybe you'll need to calculate exactly the height of padding top and bottom via temp single line TextView (before and after remove padding) , then apply decrease height result with negative padding or some Ghost Layout with translate Y. Lol
唯一有效的是
Only thing that worked is
据我所知,这是大多数小部件所固有的,并且“填充”的数量因手机制造商而异。该填充实际上是图像边框和 9 补丁图像文件中的图像之间的空白区域。
例如,在我的 Droid X 上,微调器小部件比按钮有更多的空白,这使得当您有一个内联按钮的微调器时看起来很尴尬,但在我妻子的手机上,相同的应用程序没有相同的问题,而且看起来很棒!
我唯一的建议是创建您自己的 9 个补丁文件并在您的应用程序中使用它们。
啊啊,Android 的痛苦。
编辑:澄清填充与空白。
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.
这个技巧对我有用(对于 min-sdk >= 18)。
我使用了
android:includeFontPadding="false"
和 负边距,例如android:layout_marginTop="-11dp"
并放置了我的TextView
内FrameLayout
(或任何 ViewGroup...)最后是示例代码:
This trick worked for me (for min-sdk >= 18).
I used
android:includeFontPadding="false"
and a negative margin likeandroid:layout_marginTop="-11dp"
and put myTextView
inside aFrameLayout
( or any ViewGroup...)and finally sample codes:
使用constraintlayout作为根视图,然后添加指南助手。
示例:
属性layout_constraintGuide_begin值只是示例,这取决于您的需求。
Use constraintlayout as your root view then add a guideline helper.
Example:
Attribute layout_constraintGuide_begin value is just example, it depend on your needs.
这对我有用:
This worked for me:
您可以尝试使用此属性(ConstraintLayout):
layout_constraintBaseline_toBaselineOf
You can try to use this attribute(ConstraintLayout):
layout_constraintBaseline_toBaselineOf
您可以参考下面的 XML 文件,其中 imageView 将从 Textview 文本基线的底部开始。
或者,假设对于大多数用例,您需要 imageView 的底部与基线对齐,为此您可以将以下代码放入 TextView 中。
You can take reference from the below XML file where the imageView will start from the bottom of baseline of the text of Textview.
Alternatively, assuming for most of your use cases you would need the bottom of your imageView to be aligned with the baseline, for that you can put the below code in your TextView.
这有效:
This works:
看这个:
将 ImageView 与 EditText 水平对齐
看来 EditText 的背景图片有一些透明像素也添加填充。
解决方案是将 EditText 的默认背景更改为其他内容(或者什么都不更改,但 EditText 没有背景可能是不可接受的)。这可以通过设置 android:background XML 属性来实现。
See this:
Align ImageView with EditText horizontally
It seems that the background image of EditText has some transparent pixels which also add padding.
A solution is to change the default background of EditText to something else (or nothing, but no background for a EditText is probably not acceptable). That's can be made setting android:background XML attribute.
这对我有用
只需在您的 Textview 中添加两行即可
注意:不要使用
android:textAppearance=""
It's working for me
just add two lines to your Textview
note: don't use
android:textAppearance=""
在 ImageView xml 中使用它
android:adjustViewBounds="true"
Use this in your ImageView xml
android:adjustViewBounds="true"
或者在
XML
中,这将是:设置
TextView
是否包含额外的顶部和底部填充,以便为高于正常上升和下降的重音腾出空间。默认为 true。or in
XML
this would be:Set whether the
TextView
includes extra top and bottom padding to make room for accents that go above the normal ascent and descent. The default is true.我搜索了很多正确的答案,但没有找到可以完全删除
TextView
中所有填充的答案,但最终在浏览了 官方文档解决了单行文本将这两行添加到
TextView
xml 将完成这项工作。第一个属性删除为重音符号保留的填充,第二个属性删除为保持两行文本之间的适当间距而保留的间距。
I searched a lot for proper answer but no where I could find an Answer which could exactly remove all the padding from the
TextView
, but finally after going through the official doc got a work around for Single Line TextsAdding these two lines to
TextView
xml will do the work.First attribute removes the padding reserved for accents and second attribute removes the spacing reserved to maintain proper space between two lines of text.
我感受到你的痛苦。我已经尝试了上面的每个答案,包括将
setIncludeFontPadding
设置为 false,这对我没有任何作用。我的解决方案?
TextView
上的layout_marginBottom="-3dp"
为您提供了底部的解决方案,嘭!
虽然
layout_marginTop
上的 -3dp 失败了......呃。I feel your pain. I've tried every answer above, including the
setIncludeFontPadding
to false, which did nothing for me.My solution?
layout_marginBottom="-3dp"
on theTextView
gives you a solution for the bottom,BAM!
Although, -3dp on
layout_marginTop
fails....ugh.你可以尝试使用这个属性(ConstraintLayout):
layout_constraintBaseline_toBaselineOf
像这样:
You can try to use this attribute(ConstraintLayout):
layout_constraintBaseline_toBaselineOf
Like this:
更新的 XML
Updated XML
如果您使用
AppCompatTextView
(或来自 < code>API 28 开始)您可以使用这 2 个属性的组合来删除第一行上的空格:XML
Kotlin
If you use
AppCompatTextView
( or fromAPI 28
onward ) you can use the combination of those 2 attributes to remove the spacing on the first line:XML
Kotlin
添加 android:includeFontPadding="false" 看看是否有帮助。并使文本视图大小与文本大小相同,而不是“换行内容”。它肯定会起作用。
Add android:includeFontPadding="false" to see if it helps.And make text view size same as that of text size rather than "wrap content".It will definitely work.
这也让我很恼火,我发现的答案是字体本身实际上有额外的空间,而不是 TextView。这是相当令人恼火的,因为它来自文档发布背景,Android 对排版元素的控制有限。我建议使用可能不存在此问题的自定义字体(例如 Bitstream Vera Sans,它已获得重新分发许可)。不过,我不确定具体是否如此。
This annoyed me too, and the answer I found was that there is actually additional space in the font itself, not the TextView. It is rather irritating, coming from a document publishing background, the limited amount of control you have with Android over typographic elements. I'd recommend using a custom typeface (such as Bitstream Vera Sans, which is licensed for redistribution) that may not have this issue. I'm not sure specifically whether or not it does, though.
我删除了自定义视图中的间距——NoPaddingTextView。
https://github.com/SenhLinsh/NoPaddingTextView
I remove the spacing in my custom view -- NoPaddingTextView.
https://github.com/SenhLinsh/NoPaddingTextView
由于我的要求是覆盖从
findViewById(getResources().getIdentifier("xxx", "id", "android"));
获取的现有 textView,所以我不能简单地尝试其他答案的 onDraw()
。但我只是找出解决问题的正确步骤,这是布局检查器的最终结果:
因为我想要的只是删除顶部空格,所以我不必选择其他字体删除底部空间。
这是修复它的关键代码:
第一个键是设置自定义字体“fonts/myCustomFont.otf”,它的底部有空格但顶部没有,您可以通过打开 otf 文件并单击其中的任何字体轻松地找出这一点android Studio:
如您所见,底部的光标有额外的间距,但顶部没有,因此它解决了我的问题。
第二个关键是你不能简单地跳过任何代码,否则它可能无法工作。这就是为什么您会发现有些人评论说某个答案有效,而另一些人评论说它不起作用。
让我们来说明一下如果我删除其中一个会发生什么。
没有
setTypeface(mfont);
:没有
setPadding(0, 0, 0, 0);
:不带 <代码>setIncludeFontPadding(false);:
没有其中 3 个(即原始图像):
Since my requirement is override the existing textView get from
findViewById(getResources().getIdentifier("xxx", "id", "android"));
, so I can't simply tryonDraw()
of other answer.But I just figure out the correct steps to fixed my problem, here is the final result from Layout Inspector:
Since what I wanted is merely remove the top spaces, so I don't have to choose other font to remove bottom spaces.
Here is the critical code to fixed it:
The first key is set custom font "fonts/myCustomFont.otf" which has the space on bottom but not on the top, you can easily figure out this by open otf file and click any font in android Studio:
As you can see, the cursor on the bottom has extra spacing but not on the top, so it fixed my problem.
The second key is you can't simply skip any of the code, otherwise it might not works. That's the reason you can found some people comment that an answer is working and some other people comment that it's not working.
Let's illustrated what will happen if I remove one of them.
Without
setTypeface(mfont);
:Without
setPadding(0, 0, 0, 0);
:Without
setIncludeFontPadding(false);
:Without 3 of them (i.e. the original):
通过额外的ImageView,我们可以将TextView设置为与ImageView基线对齐,并将ImageView上的
android:baselineAlignBottom
设置为true,这将使ImageView的基线位于底部。其他视图可以使用 ImageView 的底部自行对齐,该底部本身与 TextView 的基线相同。然而,这仅修复了填充底部而不是顶部。
With an extra ImageView we can set the TextView to be baseline aligned with the ImageView and set the
android:baselineAlignBottom
on the ImageView to true, which will make the baseline of ImageView to bottom. Other views can align itself using the bottom of the ImageView which itself is the same as the TextView's baseline.This however only fixes the padding bottom not the top.
我认为这个问题可以这样解决:
结果如下:
ScreenShot-1
< a href="https://i.sstatic.net/q2AoM.png" rel="nofollow noreferrer">ScreenShot-3
虽然rightLowerText中的特殊字符行看起来比第二行高一点leftText,它们的基线仍然对齐。
I think this problem can be solved in this way:
Those are the results:
ScreenShot-1
ScreenShot-3
Though the line of special characters in rightLowerText looks a little bit higher than the second line of leftText, their baselines are stilled aligned.
您可以使用此处列出的一些技巧(负边距、字体填充等)部分解决此问题,但您无法解决字体本身的大小[1]。字体的每个“单元格”的固定高度必须足够大,以容纳所有语言中最高的字符,包括上升部分和下降部分。
如果您采用其他答案中列出的技巧,字体可能最终会被剪切。
解决方案是向您的设计师解释这就是字体的打包方式,并让他们在设计中考虑到这一点。当它被翻译成其他语言并且上升/下降部分被剪掉时,不要现在就进行修改以将错误归档。
编写一个示例应用程序,只需将一系列字符放入
TextView
中,然后在其周围绘制一个框,更改背景或其他内容,以便您可以看到字体的自然填充。将其交给您的设计师,让其重新融入到他们的设计中。[1] 我想人们可以用压缩的上升部分和下降部分打包自己的字体。
You can partially resolve this with some of the hacks listed here (negative margins, font padding, etc), but you can't get around the size of the font itself[1]. The fixed height of each "cell" of a font must be large enough to accommodate the tallest character, in all languages, including ascenders and descenders.
If you employ the hacks listed in other answers the font may end up clipped.
The solution is to explain to your designers that this is how fonts are packaged, and to have them account for this in their designs. Don't hack things up now to have bugs filed down the road when it's translated to other languages and ascenders / descenders are clipped.
Write a sample app the just puts a series of characters in a
TextView
, then draw a box around it, change the background or something so you can see the natural padding of the font. Give this to your designers to incorporate back into their designs.[1] I suppose one could package their own fonts with compacted ascenders and descenders.
您可能想尝试将左侧文本视图的底部与第二个右侧文本视图的底部对齐。
You might want to try aligning the bottom of the left text view with the bottom of the 2nd right text view.