我的 textview 对象中有不必要的填充。让我们只关注下图中右上角的 TextView ,即 1.48
和 6.91
的那个。我的要求是文本绘制到其自身边界的右上角,因此它对 DIJACOR
是“顶部对齐的”,对 +0.01
是“右对齐的”。我尝试了 gravity="top|right"
、includeFontPadding=false
和 paddings=0
但无济于事。对于第二种情况,渲染结果在 6.91
之后显示了一些额外的空格,使其与下面的 -0.01
不对齐。任何人都可以与我分享如何解决这个问题?谢谢!
下面是我如何定义 textView 对象:
<TextView
android:id="@+id/priceLast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:gravity="top|right"
android:includeFontPadding="false"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:singleLine="true"
android:text="@string/price"
android:textColor="@color/dark_green"
android:textIsSelectable="false"
android:textSize="24sp"
android:textStyle="bold" />
Root View 组:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_card_selector"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingTop="4dp" >
最佳答案
对于案例 #2,原因是在大多数字体中,所有数字都具有相同的字符宽度(以像素为单位)。所以数字“1”和“8”占用相同数量的宽度像素。原因是您可以将数字写在彼此之上,并且数字将始终对齐。这意味着像“4.1”这样的字符串在它的右边比“4.8”有更多的空像素。 解决方案是使用每个数字都具有紧凑宽度的字体,或者使用您自己的 View 将数字呈现为位图,找到最右边的非空像素,然后移动位图以使其右对齐。
关于android - TextView 不粘在右上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19567675/