Android UI开发中的常用控件TextView

来源:互联网 发布:软件测试精品课程 编辑:程序博客网 时间:2024/05/17 11:58

TextView

TextView可以说是android中最简单的一个控件了, 主要是用来显示界面上的一段文本信息.

<?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:id="@+id/text_huijun"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" /></LinearLayout>

外面的LinearLayout暂且忽略, 在TextView中我们使用了android:id把当前控件定义了一个唯一的标示.
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”是指定了控件的宽度和高度.

在android中所有的空间都具有两个属性, 可选值有3中:
much_parent: 标示让当前控件的大小刚好和父布局大小一样.
fill_parent: 意义和much_parent相同, 建议使用much_parent
wrap_content: 标示让当前空间的大小刚好包含住里面的内容, 由控件的内容决定控件的大小.

android:text: 表示要显示的内容
下面是运行效果图:

当然我们也可以修改文字的对齐方式:
可选的方式有:top, bottom, left, right, center, 当然他们是可以同时使用的,中间用’|’连接. 比如center_vertical|center_horizontal
下面是center居中的效果和写法
android:layout_gravity=”center”
<TextView
android:id="@+id/text_huijun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!" />

当然我们也可以更改文字的大小和颜色:

<TextView
android:id="@+id/text_huijun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
android:text="Hello World!" />

android:textSize=”24sp”用于指定文字的大小
* android:textColor=”#00ff00”*用于指定文字的颜色
重新运行效果图:

0 0