Textview
是Android
最简单的一个控件了,主要用于再界面上显示一段文本信息。
一、代码示例
先新建个空项目,名字叫UIWidget
然后修改主布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView"
/>
</LinearLayout>
关于里面各个参数是啥意思,可以参考这篇:《安卓四大组件之活动》
二、运行实例
TextView
中的文字默认是左上角对齐的
我们修改布局,使用android:gravity
控制文字的对齐方式:
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is TextView"
/>
center
= center_vertical
+ center_horizontal
还可以对文字的大小和颜色修改:
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
android:text="This is TextView"
/>