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

android:gravity="right"

但这对我没用。

我可能做错了什么?


当前回答

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

其他回答

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

Or

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

  <TextView
    android:id="@+id/tct_postpone_hour"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:gravity="center_vertical|center_horizontal"
    android:text="1"
    android:textSize="24dp" />
</RelativeLayout>

使一号在水平和垂直的中心对齐。

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

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

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

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

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

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

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

这应该可以达到目的

android:textAlignment=“textStart”