[英]How to right-align text in an Android TextView or EditText?
I have an EditText in my application.我的应用程序中有一个 EditText。 I want to align the text in it to the right instead of the default left.
我想将其中的文本右对齐而不是默认的左对齐。 I tried adding
我尝试添加
android:layout_gravity="right"
but this doesn't seem to work.但这似乎不起作用。 any other suggestions please?
请问还有其他建议吗?
You should use android:gravity="right"
.你应该使用
android:gravity="right"
。 layout_gravity
is for the view (EditText) alignment against the container. layout_gravity
用于视图 (EditText) 与容器对齐。
You may use android:layout_alignParentRight="true"
to align the EditText's right edge to its parent's right edge.您可以使用
android:layout_alignParentRight="true"
将 EditText 的右边缘与其父级的右边缘对齐。 Also, if you really want to use the layout_gravity
or gravity
attributes, check out this article that discusses the proper use of them.另外,如果你真的想使用
layout_gravity
或gravity
属性,看看这个文章,讨论如何正确使用它们。
您可以在属性窗口中设置用于编辑文本的属性,即 Gravity 向右或通过在 UI 的 xml 文件中添加行代码:android:gravity="right"
layout _gravity means that you are specifying the layouts attribute, not the element inside. layout _gravity 意味着您指定的是layouts属性,而不是里面的元素。 In some conditions, such as layout_width and layout_height is wrap_content, layout specifications can give what you want about the element inisde your layout, since the boundries of the layout and the element (not element s ) are the same..
在某些情况下,例如 layout_width 和 layout_height 是 wrap_content,布局规范可以在布局中提供您想要的元素,因为布局和元素(不是元素s )的边界是相同的。
use layout width and height as wrap content inside relative layout :使用布局宽度和高度作为相对布局内的包装内容:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.