我有一个TextView在我的应用程序。我想让文本向右对齐。我试着补充道:
android:gravity="right"
但这对我没用。
我可能做错了什么?
我有一个TextView在我的应用程序。我想让文本向右对齐。我试着补充道:
android:gravity="right"
但这对我没用。
我可能做错了什么?
当前回答
我也面临同样的问题,并认为问题正在发生,因为TextView的layout_width是有wrap_content。你需要有layout_width为match_parent和android:gravity = "gravity"
其他回答
一般来说,android:gravity="right"和android:layout_gravity="right"是不同的。
第一个参数会影响文本本身在视图中的位置,所以如果你想让它右对齐,那么layout_width=应该是"fill_parent"或"match_parent"。
第二个影响视图在其父视图中的位置,换句话说——在父视图中对齐对象本身(编辑框或文本视图)。
确保你没有使用android:layout_width="wrap_content"如果是这样,那么它将无法设置重力,因为你没有足够的空间来对齐文本。而是使用android:layout_width="fill_parent"
在我的情况下,我使用相对布局的一个emptyString
在纠结了一个小时之后。只有这个对我有用:
android:layout_toRightOf="@id/welcome"
android:layout_toEndOf="@id/welcome"
android:layout_alignBaseline="@id/welcome"
layout_toRightOf或layout_toEndOf都可以,但为了更好地支持它,我同时使用了它们。
更清楚地说:
这就是我想做的:
这是模拟器的输出
布局:
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Welcome "
android:textSize="16sp" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/welcome"
android:layout_toRightOf="@id/welcome"
android:text="@string/emptyString"
android:textSize="16sp" />
注意:
android: layout_width = " wrap_content "的作品 不使用重力
这应该可以达到目的
android:textAlignment=“textStart”
使(LinearLayout) android:layout_width="match_parent"和TextView的android:layout_gravity="right"