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

android:gravity="right"

但这对我没用。

我可能做错了什么?


当前回答

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

android:layout_alignRight="@+id/myComponentId"

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

android:layout_marginRight="20dp"

其他回答

以下代码片段:

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

对我来说很好

我认为你是这样做的:android:layout_width = "wrap_content" 如果是这种情况,这样做:android:layout_width = "match_parent"

android:gravity="right"

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

android:layout_width = "match_parent"

and

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

android:layout_alignParentRight="true"

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

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

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

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

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

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

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

Or

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