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


当前回答

您可以在java中用程序实现这一点:textview.setGravity(Gravity.CENTER);

其他回答

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

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

对于文本的布局,最好使用

android:textAlignment=“中心”

对于对象等的其他布局,使用

android:gravity=“中心”

试试看:

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

试试看:

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

让我们在屏幕中显示全文视图

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

让我们说相对布局中的文本视图(未满)

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

假设Linear_layout中的Textview(未满)

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_gravity="centre"
    />