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


当前回答

如何将视图或其内容居中,例如TextView或Button水平和垂直?layout_gravity可用于定位视图位于其父视图的中心。当重力属性用于将视图的内容(例如“文本”)放置在视图的中心。

(1)水平居中文本视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
       android:orientation="vertical"
       android:layout_margin="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_gravity="center"
        android:background="#333"
        android:textColor="#fff"
        />

</LinearLayout>

(2)垂直居中文本视图

android:layout_gravity=“center_vertical”不起作用!!

要使TextView垂直居中,您需要做一个技巧。将TextView放置在RelativeLayout中,然后设置TextView属性android:layout_centerInParent=“true”

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
       android:orientation="vertical"
       android:layout_margin="10dp"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerInParent="true"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:background="#333"
        android:textColor="#fff"
        />

</RelativeLayout>

(3)水平居中文本

android:gravity=“center_horizontal”

(4)水平和垂直居中文本

android:gravity=“center”这将使文本水平居中垂直地

<文本视图android:layout_width=“fill_parent”android:layout_height=“100dp”android:text=“Hello World!”android:gravity=“中心”android:background=“#333”android:textColor=“#fff”/>

其他回答

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);

您可以这样做以使文本居中

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

还可以使用以下方法动态设置:

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

android:gravity=“center_horizontal”表示文本水平居中。android:gravity=“center_vertical”用于垂直对齐文本中心。android:gravity=“center”用于垂直和水平对齐文本中心。

<TextView
        android:layout_width="match_parent"
        android:gravity="center_horizontal|center_vertical"
        android:layout_height="match_parent" />

文本视图宽度和高度应为match_parent,并在文本视图属性中添加android:gravity=“center”。