如何在Android中水平居中(RelativeLayout)?

如何在Android中水平居中(RelativeLayout)?,android,android-layout,Android,Android Layout,我有以下代码: <TextView android:id="@+id/exercise_name_label" android:layout_width="match_parent" android:layout_height="32dp" android:text="Curls" android:gravity="bottom" android:layout_centerHorizontal="true" /> 我希望文

我有以下代码:

<TextView
    android:id="@+id/exercise_name_label"
    android:layout_width="match_parent"
    android:layout_height="32dp"
    android:text="Curls"
    android:gravity="bottom"
    android:layout_centerHorizontal="true"
    />

我希望文本在32dp的底部对齐,水平居中。目前它不是水平居中:

选项1: 您应该将重心调整为水平中心(底部=)


备选案文2: 您应该调整宽度以在父对象中水平环绕内容并居中)


android:layout\u centerInParent=“true”就是您想要的。但我必须说,你不应该使用相对布局。在我看来,线性布局更稳定


如果要将其更改为线性布局,可以使用android:Layout\u gravity=“center”将这些属性设置为您的文本视图:

<!-- It will make your layout center horizontal relative to parent -->
android:layout_centerHorizontal="true"

<!-- It will draw your text from center of the area assigned to textview-->
android:gravity="center"

android:layout\u centerHorizontal=“true”
android:gravity=“center”

请给我看一下选项2的代码好吗?我试过了,但没能成功
<TextView
    android:id="@+id/exercise_name_label"
    android:layout_width="wrap_content"
    android:layout_height="32dp"
    android:text="Curls"
    android:gravity="bottom"
    android:layout_centerHorizontal="true" />
<!-- It will make your layout center horizontal relative to parent -->
android:layout_centerHorizontal="true"

<!-- It will draw your text from center of the area assigned to textview-->
android:gravity="center"