【发布时间】:2014-03-19 00:27:26
【问题描述】:
我正在尝试在圆圈内绘制一个数字,但在文本居中时遇到问题。以下是我用来显示 TextView 的 xml
<RelativeLayout
android:id="@+id/label"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_margin="8dip" >
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hole_background"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/hole"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/white"
android:textSize="56sp"
android:textStyle="bold"
android:gravity="center"/>
</RelativeLayout>
问题是它只是稍微靠近底部(在我测试的设备上大约 10 像素)。这是视图层次结构查看器的屏幕截图,显示了我在说什么...
我尝试合并视图并使用背景,例如here,或者像建议的here 那样删除字体填充,但都没有奏效,字体填充如下所示...
我目前正在使用负边距来调整它以使其看起来正确,但这似乎不是正确的方法。有没有人知道如何使文本居中而不使用我必须根据文本大小手动检查的负边距?
提前致谢
【问题讨论】:
-
这很可能是字体指标问题——居中不是基于实际字形,而是基于文本基线和垂直范围。想象一下,如果将“a”或“.”垂直居中会发生什么情况。 -- 显然,它们不会在中心,而是在圆的中心线下方,因此大写字符会出现更多居中。我不知道除了尝试不同的字体或手动更正之外,您真的可以做任何事情。
-
这很有道理,我并没有真正考虑不同的字符大小将如何发挥作用。由于我有一个非常具体的用例,我刚刚创建了一个自定义视图,它根据正在绘制的文本的实际高度处理居中。感谢您的输入
标签: android textview centering android-fonts