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

android:gravity="right"

但这对我没用。

我可能做错了什么?


当前回答

更好的解决方案是最简单的解决方案,并且对代码行为的修改较少。

如果你能解决这个问题只有2个属性在你的TextView?

而不是需要改变你的线性布局属性,也许可以改变行为的线性布局子?

使用这种方式,你不需要改变线性布局的属性和行为,你只需要添加以下两个属性到你的目标TextView:

android:gravity="right"
android:textAlignment="gravity"

如果只改变你的目标来解决你的问题,而不是有机会在未来引起另一个问题,修改你的目标父亲,会更好吗?想想吧。

其他回答

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

以下代码片段:

android:textAlignment="textEnd"
android:layout_gravity="end"

对我来说很好

android:gravity="right"

它基本上用于线性布局,所以你必须设置你的宽度的TextView或编辑文本为

android:layout_width = "match_parent"

and

当你在相对布局中工作时,使用

android:layout_alignParentRight="true"

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

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

缺省情况下,value为none。

Android TextView文档

关于Android TextView的问题

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