如何在Android的TextView或EditText中右对齐文本?

56 人关注

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

android:layout_gravity="right"

但这似乎并不奏效。请问还有什么建议吗?

android
alignment
android-edittext
layout-gravity
sherry
sherry
发布于 2011-04-18
5 个回答
Randy Sugianto 'Yuku'
Randy Sugianto 'Yuku'
发布于 2020-10-24
已采纳
0 人赞同

你应该使用 android:gravity="right" 。替换代码1】是用于视图(EditText)与容器的对齐。

奇怪的是,一旦你做了这个引力=right,提示就会停止工作。
对于有网络经验的人来说,android:gravity的工作原理类似于text-align:center,android:layout_gravity:left|bottom的工作原理类似于float:left|bottom。
Erick
Erick
发布于 2020-10-24
0 人赞同

你可以使用 android:layout_alignParentRight="true" 来使EditText的右边缘与它的父的右边缘对齐。另外,如果你真的想使用 layout_gravity gravity 属性,请查看这个 文章 该书讨论了如何正确使用它们的问题。

krupa parekh
krupa parekh
发布于 2020-10-24
0 人赞同

你只需在属性窗口中设置编辑文本的属性,即重力向右,或在用户界面的xml文件中加入一行代码:android:gravity="right"

Alpaslan
Alpaslan
发布于 2020-10-24
0 人赞同

布局 _gravity的意思是,你正在指定 布局s attribute, not the element inside. In some conditions, such as 布局_width and 布局_height is wrap_content, 布局 specifications can give what you want about the element inisde your 布局, since the boundries of the 布局 and the element (not element s )是一样的。

that
that
发布于 2020-10-24
0 人赞同

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

<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"