Android文本对齐 - 像素完美

4
在下面的截图中,我试图让蓝色框中的文本居中对齐。它太低了,需要上移一两个像素。我一直在调整填充,但似乎没有任何效果。
布局XML在此处:
<RelativeLayout  xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginRight="5dp"
xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView android:id="@+id/tvName" 
        android:layout_width="60dp" 
        android:layout_height="20dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="3dp" 
        android:background="@drawable/rectanglesegment" 
        android:gravity="center"
        android:paddingBottom="2dp" 
        android:text="XXX" android:textSize="16sp" 
        android:textAppearance="?android:attr/textAppearanceMedium"/>

有什么建议吗?

@Snicolas 的反馈后,我现在有以下内容:

enter image description here

采用这种布局。

<TextView android:id="@+id/tvName" 
android:layout_width="60dp" 
android:layout_height="fill_parent" 
android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp" 
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:background="@drawable/rectanglesegment" 
android:gravity="center"
android:text="abv" android:textSize="16sp" 
android:textAppearance="?android:attr/textAppearanceMedium"/>

paddingTop -2 没有起作用吗? - Snicolas
你也可以尝试使用android:capitalize = true。 - Snicolas
1
这个线程适合你: https://dev59.com/mXRC5IYBdhLWcg3wCMg6 - Snicolas
@Snicolas,它已经使用了android:gravity="center"... - quietmint
你可以接受自己的答案并采纳它。这样做很好,对于有相同问题的其他人来说也更有用。 - Snicolas
显示剩余2条评论
3个回答

2

看起来顶部的额外空间只是用于字体填充(例如用于重音符号等)。请尝试以下操作:

<TextView
    ...
    android:includeFontPadding="false"
/>

0

0
你应该看一下背景图片 - rectanglesegment。它很可能是9 patch图像,需要进行调整。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接