I do have a problem with TextView
. I don't want to have any margin/padding above it.
我确实遇到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"/>
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?
我的TextView看起来像这样,尽管textSize和height设置为相同的值,但字体上方还有一个空格。这让我感到困扰,因为我想将另一个视图放在相对于字体顶部的位置。这个间距是否包含在字体本身中?
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)
还有一个问题:如果我发现距离顶部20dp和底部7dp的余量在我的设备上完美运行,我可以依赖它在其他屏幕上的行为方式相似吗? (这些边距适用于按钮)
4 个解决方案
#1
43
using android:includeFontPadding="false"
helped me a lot in a similar situation.
使用android:includeFontPadding =“false”在类似的情况下帮助了我很多。
#2
9
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's onDraw
method and to adjust the canvas for the difference between the font metrics' top and ascent values:
我有同样的问题,设置android:includeFontPadding = false没有帮助。我能在合理的时间内找到的最佳解决方案是覆盖TextView的onDraw方法,并调整画布以获取字体度量的top和ascent值之间的差异:
FontMetricsInt fontMetricsInt;
@Override
protected void onDraw(Canvas canvas) {
if (adjustTopForAscent){
if (fontMetricsInt == null){
fontMetricsInt = new FontMetricsInt();
getPaint().getFontMetricsInt(fontMetricsInt);
}
canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
}
super.onDraw(canvas);
}
#3
2
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.
你需要做的是将另一个视图放在相对于字体顶部的位置,并在dip中给它一个负面的android:layout_marginBottom,这样它就匹配字体的顶部。如果字体有边距,我认为没有更好的方法。
#4
1
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:
它不是一个好主意,但你可以像下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffff">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ptp_hour"
android:textColor="@android:color/black"
android:textSize="100sp"
android:lineSpacingMultiplier="0.8"
android:scrollY="-100dp"
android:scrollX="100dp"
android:text="10"/>
<TextView
android:text="10"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#00FFFFFF"
android:shadowColor="#000000"
android:shadowDy="-50"
android:shadowRadius="1"
android:textSize="100dp"/>
</LinearLayout>
Here, first "10" is of your properties and second one is as i have set for you.
在这里,第一个“10”是你的属性,第二个是我为你设置的。
Enjoy. :))
#1
43
using android:includeFontPadding="false"
helped me a lot in a similar situation.
使用android:includeFontPadding =“false”在类似的情况下帮助了我很多。
#2
9
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's onDraw
method and to adjust the canvas for the difference between the font metrics' top and ascent values:
我有同样的问题,设置android:includeFontPadding = false没有帮助。我能在合理的时间内找到的最佳解决方案是覆盖TextView的onDraw方法,并调整画布以获取字体度量的top和ascent值之间的差异:
FontMetricsInt fontMetricsInt;
@Override
protected void onDraw(Canvas canvas) {
if (adjustTopForAscent){
if (fontMetricsInt == null){
fontMetricsInt = new FontMetricsInt();
getPaint().getFontMetricsInt(fontMetricsInt);
}
canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
}
super.onDraw(canvas);
}
#3
2
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.
你需要做的是将另一个视图放在相对于字体顶部的位置,并在dip中给它一个负面的android:layout_marginBottom,这样它就匹配字体的顶部。如果字体有边距,我认为没有更好的方法。
#4
1
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:
它不是一个好主意,但你可以像下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffff">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ptp_hour"
android:textColor="@android:color/black"
android:textSize="100sp"
android:lineSpacingMultiplier="0.8"
android:scrollY="-100dp"
android:scrollX="100dp"
android:text="10"/>
<TextView
android:text="10"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#00FFFFFF"
android:shadowColor="#000000"
android:shadowDy="-50"
android:shadowRadius="1"
android:textSize="100dp"/>
</LinearLayout>
Here, first "10" is of your properties and second one is as i have set for you.
在这里,第一个“10”是你的属性,第二个是我为你设置的。
Enjoy. :))