XML文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
这样的效果图如下:
http://hi.youkuaiyun.com/attachment/201009/26/0_1285518647bs04.gif
修改后XML文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center"
/>
</LinearLayout>
修改后的效果图如下:
http://hi.youkuaiyun.com/attachment/201009/26/0_1285518806bQ4p.gif
本文介绍了如何通过XML布局文件将TextView的文字居中显示。示例代码展示了在LinearLayout中设置TextView的gravity属性为"center",以达到文字水平居中的效果,并附有修改前后的截图对比。
5504





