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


当前回答

字体填充也会影响文本的位置,您可以通过添加includefontpadding=false来删除它

<TextView
    android:includeFontPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />

其他回答

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_centerInParent="true"

以及其他布局

android:gravity="center" 

TextView的高度和宽度是换行内容,然后文本视图中的文本始终居中,然后使用以下命令在父布局中居中:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello.."/>
</RelativeLayout>

对于LinearLayout,代码也相同:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello.."/>
</LinearLayout>

从语法上讲,父级是RelativeLayout java代码,这在运行时使用类似的代码

TextView textView1 =(TextView)findViewById(R.id.texView1);
RelativeLayout.LayoutParams layoutParams = RelativeLayout.LayoutParams)textView1.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
textView1.setLayoutParams(layoutParams);

在我看来,

android:gravity="center"

优于,

android:layout_centerInParent="true"

这比,

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

至少用于格式化文本。

android:gravity="center" 

这就行了