android - TextView 不粘在右上角

标签 android android-layout user-interface textview padding

我的 textview 对象中有不必要的填充。让我们只关注下图中右上角的 TextView ,即 1.486.91 的那个。我的要求是文本绘制到其自身边界的右上角,因此它对 DIJACOR 是“顶部对齐的”,对 +0.01 是“右对齐的”。我尝试了 gravity="top|right"includeFontPadding=falsepaddings=0 但无济于事。对于第二种情况,渲染结果在 6.91 之后显示了一些额外的空格,使其与下面的 -0.01 不对齐。任何人都可以与我分享如何解决这个问题?谢谢!

grid view object

下面是我如何定义 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/

相关文章:

android - 如何根据它覆盖的 ImageView 的颜色更改文本的颜色?

android - 如何设置android :orientation ="horizontal" programmatically in RelativeLayout?

android - 使用 PhoneGap 时将标题添加回 Android 窗口

ios - 栏按钮项目无法执行操作 Swift

android - 查看在其他布局后面的动画

android - Activity 太多会影响我的表现吗?

java - GPS 不适用于短距离

Java Slider - 如何制作自定义刻度?

android - 更新当前使用的应用程序

android - 如何在 Kotlin 中注入(inject)原始变量?