简体   繁体   English

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

[英]How do I horizontally center this in Android (RelativeLayout)?

I have the following code: 我有以下代码:

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

And I want the text to be aligned at the bottom of the 32dp and center horizontally..currently it's not centering horizontally: 我希望文本在32dp的底部对齐并水平居中..当前它不在水平居中:

在此处输入图片说明

OPTION 1: You should adjust gravity to center_horizontal|bottom =) 选项1:您应将重力调整为center_horizo​​ntal | bottom =)

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

OPTION 2: You should adjust width to wrap_content and center_horizontally in parent =) 选项2:您应将父控件的宽度调整为wrap_content和center_horizo​​ntally =

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

android:layout_centerInParent="true" is what you want. android:layout_centerInParent="true"是您想要的。 But I must say that you should not use Relative Layout. 但是我必须说您不应该使用相对布局。 Linear Layout is more steady in my opinion. 我认为线性布局更稳定。

If you want to change it to Linear Layout you can use android:layout_gravity="center" . 如果要将其更改为线性布局,可以使用android:layout_gravity="center"

Set these attribute to your TextView: 将这些属性设置为您的TextView:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM