[英]How to right-align text in an Android TextView or EditText?
我的應用程序中有一個 EditText。 我想將其中的文本右對齊而不是默認的左對齊。 我嘗試添加
android:layout_gravity="right"
但這似乎不起作用。 請問還有其他建議嗎?
你應該使用android:gravity="right"
。 layout_gravity
用於視圖 (EditText) 與容器對齊。
您可以使用android:layout_alignParentRight="true"
將 EditText 的右邊緣與其父級的右邊緣對齊。 另外,如果你真的想使用layout_gravity
或gravity
屬性,看看這個文章,討論如何正確使用它們。
您可以在屬性窗口中設置用於編輯文本的屬性,即 Gravity 向右或通過在 UI 的 xml 文件中添加行代碼:android:gravity="right"
layout _gravity 意味着您指定的是layouts屬性,而不是里面的元素。 在某些情況下,例如 layout_width 和 layout_height 是 wrap_content,布局規范可以在布局中提供您想要的元素,因為布局和元素(不是元素s )的邊界是相同的。
使用布局寬度和高度作為相對布局內的包裝內容:
<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.