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


当前回答

这是解决方案。

<TextView  
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center"
       android:text="**Your String Value**" />

其他回答

我假设您使用的是XML布局。

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>

还可以根据需要使用重力center_vertical或center_horizontal。

正如@steelthcopter在java:.setGravity(Gravity.CENTER);中评论的那样;。

对于Kotlin用户,.egravity=重力.CENTER

如果XML文件中只有一个TextView,则可以将父视图重心作为中心。这将使您的子元素位于屏幕的中心,然后您可以将您的子视图作为高度和宽度的环绕。

在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>

如何将视图或其内容居中,例如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”/>

在我看来,

android:gravity="center"

优于,

android:layout_centerInParent="true"

这比,

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

至少用于格式化文本。