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

android:gravity="right"

但这对我没用。

我可能做错了什么?


当前回答

确保你没有使用android:layout_width="wrap_content"如果是这样,那么它将无法设置重力,因为你没有足够的空间来对齐文本。而是使用android:layout_width="fill_parent"

其他回答

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

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

缺省情况下,value为none。

Android TextView文档

关于Android TextView的问题

这应该可以达到目的

android:textAlignment=“textStart”

尝试添加android:gravity="center"到TextView

使(LinearLayout) android:layout_width="match_parent"和TextView的android:layout_gravity="right"

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

android:layout_alignRight="@+id/myComponentId"

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

android:layout_marginRight="20dp"