参考自《疯狂android讲义》2.3节
//TextView所呈现的文字
android:text="我爱Java"
//文字颜色
android:textColor="#f00"
//文字尺寸
android:textSize="20pt"
//文本框结尾处绘制图片
android:drawableEnd="@drawable/ic_launcher"
//不管内容多长,单行显示
android:singleLine="true"
//文字过长时,中间部分省略
android:ellipsize="middle"
//全部字母大写
android:textAllCaps="true"
//若文字为email或者电话号码,以特殊形式呈现
android:autoLink="email|phone"
//文字为密码,以点代替
android:password="true"
//文字阴影相关
android:shadowColor="#0000ff"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
//指定背景图案
android:background="@drawable/bg_border"
实例一:TextView的常用属性
<?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"
>
<!-- 设置字体为20pt,文本框结尾处绘制图片 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我爱Java"
android:textSize="20pt"
android:drawableEnd="@drawable/ic_launcher"
/>
<!-- 设置中间省略, 所有字母大写 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我aaaJava"
android:ellipsize="middle"
android:textAllCaps="true"
/>
<!-- 对邮件、电话增加链接 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="邮件是kongyeeku@163.com,电话是02088888888"
android:autoLink="email|phone"
/>
<!-- 设置文字颜色 、大小,并使用阴影 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试文字"
android:shadowColor="#0000ff"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:textColor="#f00"
android:textSize="18pt"
/>
<!-- 测试密码框 -->
<TextView android:id="@+id/passwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:password="true"
/>
<!-- 测试CheckedTextView
通过checkMark设置该文本框的勾选图标
-->
<CheckedTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="可勾选的文本"
android:checkMark="@drawable/ok"
/>
</LinearLayout>
实例二:使用xml文件指定drawable资源,并用之于TextView的背景
<?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" >
<TextViewandroid:layout_width="match_parent" android:layout_height="wrap_content"android:text="带边框的文本"android:textSize="24pt"android:background="@drawable/bg_border"/><!-- 通过android:drawableLeft绘制一张图片 --><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:text="圆角边框、渐变背景的文本"android:textSize="24pt"android:background="@drawable/bg_border2"/></LinearLayout><!-- 通过android:background指定背景 -->
bg_border.xml<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 设置背景色为透明色 --> <solid android:color="#0000"/> <!-- 设置红色边框 --> <stroke android:width="4px" android:color="#f00" /> </shape>
bg_border2.xml<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 指定圆角矩形的4个圆角的半径 --> <corners android:topLeftRadius="20px" android:topRightRadius="5px" android:bottomRightRadius="20px" android:bottomLeftRadius="5px"/> <!-- 指定边框线条的宽度和颜色 --> <stroke android:width="4px" android:color="#f0f" /> <!-- 指定使用渐变背景色,使用sweep类型的渐变 颜色从红色→绿色→蓝色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"/> </shape>