【问题标题】:How to right-align text in an Android TextView or EditText?如何在 Android TextView 或 EditText 中右对齐文本?
【发布时间】:2011-08-07 14:02:43
【问题描述】:

我的应用程序中有一个 EditText。我想将其中的文本右对齐而不是默认的左对齐。我尝试添加

android:layout_gravity="right"

但这似乎不起作用。请问还有什么建议吗?

【问题讨论】:

    标签: android alignment android-edittext layout-gravity


    【解决方案1】:

    您应该使用android:gravity="right"layout_gravity 用于视图(EditText)与容器对齐。

    【讨论】:

    • 奇怪的是,一旦您执行此重力=right,提示就会停止工作
    • 适合有网络经验的人。 android:gravity 的作用类似于 text-align:center 和 android:layout_gravity:left|bottom 的作用类似于 float:left|bottom
    【解决方案2】:

    您可以使用android:layout_alignParentRight="true" 将 EditText 的右边缘与其父对象的右边缘对齐。此外,如果您真的想使用layout_gravitygravity 属性,请查看讨论正确使用它们的article

    【讨论】:

      【解决方案3】:

      您可以在属性窗口中设置属性以编辑文本,即重力向右或通过在 UI 的 xml 文件中添加行代码:android:gravity="right"

      【讨论】:

        【解决方案4】:

        layout_gravity 表示您指定的是 layouts 属性,而不是里面的元素。在某些情况下,例如 layout_width 和 layout_height 是 wrap_content,布局规范可以提供您想要的布局中的元素,因为布局和元素(不是元素s)的边界是相同的..

        【讨论】:

          【解决方案5】:

          使用布局宽度和高度作为相对布局内的包装内容:

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
          
              <ImageView
                  android:id="@+id/icon"
                  android:layout_width="64dp"
                  android:layout_height="64dp"
                  android:layout_alignParentStart="true"
                  android:paddingEnd="16dp"
                  android:paddingStart="8dp"
                  tools:src="@mipmap/ic_launcher"
                  android:layout_alignParentLeft="true"
                  android:paddingRight="16dp"
                  android:paddingLeft="8dp"
                  android:contentDescription="@string/todo" />
          
              <TextView
                  android:id="@+id/title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_toEndOf="@id/icon"
                  android:maxLines="1"
                  android:lines="1"
                  android:paddingTop="8dp"
                  tools:text="Google"
                  android:layout_toRightOf="@id/icon" />
          
              <TextView
                  android:id="@+id/last_used"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:layout_alignParentTop="true"
                  android:paddingTop="8dp"
                  tools:text="Last used: yesterday at 18.54" />
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 2012-02-16
            • 2013-08-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多