我有一个TextView在我的应用程序。我想让文本向右对齐。我试着补充道:

android:gravity="right"

但这对我没用。

我可能做错了什么?


当前回答

试着使用:-

android:justificationMode="inter_word"

这将证明你的短信是正确的。

其他回答

如果它是RelativeLayout,你可以使用android:layout_toRightOf="@id/<id_of_desired_item>"

Or

如果你想对齐到设备的右角的地方android:layout_alignParentRight="true"

校正对齐已从Android API 26+支持TextView

<TextView ...
    ... android:justificationMode="inter_word" />

缺省情况下,value为none。

Android TextView文档

关于Android TextView的问题

在我的情况下,我使用相对布局的一个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:layout_width = "wrap_content" 如果是这种情况,这样做:android:layout_width = "match_parent"

在我的例子中,我使用了属性“layout_alignRight”,在文本中:

android:layout_alignRight="@+id/myComponentId"

如果你想添加一些右边框,只需要设置属性“Layout_Margin right”为“20dp”,例如:

android:layout_marginRight="20dp"