介绍
TextView直接继承了View,它还是EditText、Button两个UI组件类的父类。
TextView和EditView均能在界面上显示文本,只是后者还能提供对此显示文本的编辑功能。
用例1:基本属性设置
<?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">
<!--设置字体为20pt-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一代人"
android:textSize="20pt"/>
<!--设置中间省略-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="黑夜给了我黑色的眼睛,我却用它寻找光明。"
android:ellipsize="middle"/>
<!--对邮件增加链接-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="个人邮箱yuanguangyu1221@163.com,欢迎来信。"
android:autoLink="email"/>
<!--设置文字颜色、大小,并使用阴影-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="诗歌爱好者"
android:shadowColor="#0000ff"
android:shadowDx="15.0"
android:shadowDy="20.0"
android:textColor="#ff0000"
android:textSize="25pt"/>
<!--测试密码框-->
<TextView
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:password="true"/>
</LinearLayout>
相应地界面展示结果:
用例2给文本框添加边框或图片
使用shape在drawable目录下创建一个背景文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#ff0000" />
</shape>
<?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">
<!--通过android:backgroup指定背景-->
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_border"
android:text="带边框的文本"
android:textSize="25dp"/>
<!--通过android:drawableLeft绘制一张图片-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带图片的文本"
android:drawableLeft="@drawable/leaf"/>
</LinearLayout>
相应地界面展示结果:
用例3添加默认提示和焦点切换功能,对于电话号码框时输入法自动切换到数字键盘
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名: "
android:textSize="10pt"
android:background="@drawable/bg_border"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录账号"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码: "
android:textSize="10pt"
android:background="@drawable/bg_border"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码"
android:background="@drawable/bg_border"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"/>
</TableRow>
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"/>
</TableRow>
</TableLayout>
相应地界面展示结果:
摘自《疯狂Android讲义》