如何在TextView中水平和垂直居中文本,以便它正好出现在Android中的TextView的中间?


当前回答

TextView重力按照父布局工作。

线条布局:

如果使用LinearLayout,则会发现两个重力属性安卓:重力&安卓:布局重力

android:gravity:表示TextView内部文本的布局部分android:layout_gravity:表示父视图中的TextView位置。

如果要将文本设置为水平居中和垂直居中,请使用以下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@android:color/background_light"
    android:layout_height="300dp">

    <TextView
        android:layout_width="match_parent"
        android:text="Hello World!"
        android:gravity="center_horizontal"
        android:layout_gravity="center_vertical"
        android:layout_height="wrap_content"
    />
</LinearLayout>

相对布局:

使用RelativeLayout,可以在TextView中使用以下属性

android:gravity=“center”表示TextView中的文本中心。

android:gravity=“center_horizontal”内部文本(如果需要水平居中)。

android:gravity=“center_vertical”内部文本,如果您希望垂直居中。

android:layout_centerInParent=“true”如果您希望TextView位于父视图的中心位置。android:layout_centerHorizontal=“true”如果您希望TextView位于父视图的水平中心。android:layout_centerVertical=“true”如果您希望TextView位于父视图的垂直中心。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:background="@android:color/background_light"
    android:layout_height="300dp">

    <TextView
        android:layout_width="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
    />
</RelativeLayout>

其他回答

您需要像这样设置文本视图重力(水平居中和垂直居中):

android:layout_centerHorizontal="true"   

android:layout_centerVertical="true"

动态使用:

textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

在RelativeLayout中,它会很好。

还有另一个按钮和任何你可以添加的东西。

以下内容对我很有用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff314859"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">
    <TextView 
        android:id="@+id/txt_logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your text here"
        android:textSize="30dp"
        android:gravity="center"/>

        ...other button or anything else...

</RelativeLayout>

迟到的GUI荣誉奖

我想为这个已经非常棒的线程增加一些价值。最后,在11年后,我们现在有了一种方法,可以从工具箱中拖放TextView,并通过GUI的财产框选择重力。幸运的是,GUI将财产简洁地组合为“center_vertical | center_horizontal”。

    <TextView
    android:text="This is my sample text."
    android:layout_width="match_parent"
    android:layout_height="35.7dp"
    android:id="@+id/textView2"
    android:layout_marginBottom="24.7dp"
    android:typeface="sans"
    android:textSize="20"
    android:gravity="center_vertical|center_horizontal" />

使用下面的xml代码,它对我有用,你可以改变它在中心的方向

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/id_text"
            android:layout_width="wrap_content"
            android:textSize="18sp"
            android:textColor="@android:color/white"
            android:textStyle="normal"
            android:layout_height="wrap_content"
            android:text="centre me"/>
    </LinearLayout>

在任何视图的ConstraintLayout中:

(使用约束布局时,将高度和宽度设置为0,并让约束管理视图边界。)

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />