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


当前回答

如果TextView的高度和宽度是换行内容,则TextView中的文本始终居中。但如果TextView的宽度为match_parent,高度为match-parent或wrap_content,则必须编写以下代码:

对于RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</RelativeLayout>

对于LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

其他回答

如果您试图在TableLayout中的TableRow上居中文本,以下是我实现这一点的方法:

<TableRow android:id="@+id/rowName"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dip" >
    <TextView android:id="@+id/lblSomeLabel"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:layout_width="0dp"
              android:layout_weight="100"
              android:text="Your Text Here" />
</TableRow>

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>

只需将其添加到xml中。

android:gravity="center" 

您也可以使用以下组合:

android:gravity="left|center"

然后,如果文本视图宽度大于“fill_paint”,则文本仍将向左对齐(不居中,因为重力仅设置为“center”)。

正如许多答案所表明的那样,上述工作正常。

android:gravity="center"

如果要使其垂直居中:

android:gravity="center_vertical"

或仅水平:

android:gravity="center_horizontal"