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

android:gravity="right"

但这对我没用。

我可能做错了什么?


当前回答

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

其他回答

我认为你是这样做的: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"

以下代码片段:

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

对我来说很好

试着使用:-

android:justificationMode="inter_word"

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

<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>

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