我的应用程序中有一个EditText。我想将其文本对齐到右侧,而不是默认的左侧。我尝试添加
android:layout_gravity="right"
但是这似乎不起作用。请问还有其他建议吗?
我的应用程序中有一个EditText。我想将其文本对齐到右侧,而不是默认的左侧。我尝试添加
android:layout_gravity="right"
但是这似乎不起作用。请问还有其他建议吗?
android:gravity="right"
。而layout_gravity
用于视图(EditText)相对于容器的对齐方式。android:layout_alignParentRight="true"
将EditText的右边缘与其父元素的右边缘对齐。此外,如果您真的想使用layout_gravity
或gravity
属性,请查看此文章,它讨论了它们的正确使用方法。您可以在属性窗口中设置编辑文本的属性,例如Gravity属性设置为right,或者在UI的XML文件中添加一行代码:android:gravity="right"
layout_gravity表示您正在指定布局属性,而不是其中的元素。在某些条件下,例如layout_width和layout_height为wrap_content时,布局规范可以提供关于布局内部元素所需的信息,因为布局和元素(而不是元素们)的边界相同。
在相对布局中使用布局宽度和高度作为包裹内容:
<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>