在Android中,可以使用Gravity属性来让文字居中显示。以下是两种常用的方法:
1. 使用TextView的属性:
```xml
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Hello World!" /> ``` 在上述示例中,通过设置`android:gravity="center"`属性来使文字居中显示。 2. 使用RelativeLayout或FrameLayout布局: ```xml android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Hello World!" /> ``` 或 ```xml android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Hello World!" /> ``` 以上两个示例中,通过设置相应的布局属性`android:layout_centerInParent="true"`(RelativeLayout)或`android:layout_gravity="center"`(FrameLayout)来使TextView居中显示。 以上方法任选一种即可实现文字居中显示,根据实际情况选择使用。