如何去掉TextView的上边距?
我的 TextView
确实有问题。我不想在其上方有任何边距/填充。
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ptp_hour"
android:textColor="@color/black"
android:textSize="100dp"
android:height="100dp"
android:layout_marginTop="0dp"
android:paddingTop="0dp"
android:includeFontPadding="false"
android:maxLines="1"
android:text="10"/>
我的 TextView
看起来像这样,尽管 textSize
和 height
设置为相同的值,但字体上方有一个空格。这让我很困扰,因为我想相对于字体顶部放置另一个视图。这个间距是否包含在字体本身中?
还有一个问题:如果我发现顶部距 20dp 和底部距 7dp 的边距在我的设备上完美运行,我可以相信它在其他屏幕上也会有类似的行为吗? (这些边距用于按钮)
I do have a problem with TextView
. I don't want to have any margin/padding above it.
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ptp_hour"
android:textColor="@color/black"
android:textSize="100dp"
android:height="100dp"
android:layout_marginTop="0dp"
android:paddingTop="0dp"
android:includeFontPadding="false"
android:maxLines="1"
android:text="10"/>
My TextView
looks like this and despite the textSize
and height
are set to the same value, there is a space above font. It bothers me because I want to put another view relatively to the top of the font. Is this spacing included into font itself?
And another question: If I found out that margin 20dp from top and 7dp from bottom works perfectly on my device, can I rely that it will behave in a similar way on other screens?
(these margins are for buttons)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在类似的情况下使用
android:includeFontPadding="false"
对我帮助很大。using
android:includeFontPadding="false"
helped me a lot in a similar situation.我遇到了同样的问题,设置 android:includeFontPadding=false 没有帮助。我能在合理的时间内找到的最佳解决方案是重写 TextView 的 onDraw 方法并根据字体指标的顶部值和上升值之间的差异调整画布:
I had the same issue where setting
android:includeFontPadding=false
did not help. The best solution I could find in reasonable time was to override the TextView'sonDraw
method and to adjust the canvas for the difference between the font metrics' top and ascent values:你需要做的就是将另一个视图相对于字体的顶部放置,并在dip中给它一个负的
android:layout_marginBottom
,使其与字体的顶部匹配。如果字体有边距,我认为没有更好的方法。What you need to do is to put the other view relative to the top of the font, and give it a negative
android:layout_marginBottom
in dip, such that it matches the top of the font. If the font has a margin, I don't think there is a better way of doing it.是的,默认情况下包含此空间。您无法根据我的搜索区域删除该空间。因此,您需要实现一些逻辑才能获得这样的视图。
请参见下图:
这不是一个好主意,但您可以像下面的代码一样:
这里,第一个“10” “是你的财产,第二个是我为你设置的。
享受。 :))
Yes this space included by default. You are not able to remove that space as per my search area. So you need to have to implement some logic to have such view.
See below Image:
Its not a good idea but you can do like below code:
Here, first "10" is of your properties and second one is as i have set for you.
Enjoy. :))