TextView是View的子类,用于显示文本信息,而EditText都是TextView的子类,用于接收用户输入的数据。
添加四个标签,显示四种不同样式
设置内容、字体颜色、字号、样式
对应的属性:text、textColor、textSize、textStyle
(1)修改布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/normal"
android:textColor="@android:color/holo_blue_bright"
android:textSize="15sp"
android:textStyle="normal" />
<TextView
android:id="@+id/tv_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bold"
android:textColor="@android:color/holo_green_light"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/italic"
android:textColor="@android:color/holo_orange_light"
android:textSize="15sp"
android:textStyle="italic" />
<TextView
android:id="@+id/tv_bold_italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bold_italic"
android:textColor="@android:color/holo_purple"
android:textSize="15sp"
android:textStyle="bold|italic" />
</LinearLayout>
(2)修改strings.xml文件
<resources>
<string name="app_name">0601Demo</string>
<string name="action_settings">设置</string>
<string name="normal">常规:Welcome to Android World</string>
<string name="bold">粗体:Welcome to Android World</string>
<string name="italic">斜体:Welcome to Android World</string>
<string name="bold_italic">粗斜:Welcome to Android World</string>
</resources>
运行程序,看看效果:
任务2、添加分隔线,实现分组效果
在实际项目开发中,经常会用到水平或垂直分割线,达到界面逻辑分块的目的。
<!--分割线-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#aaaaaa" />
运行效果:
任务3、添加一个标签,将所有字母自动转换成大写
(1)修改activity_main.xml
添加:
<TextView
android:id="@+id/tv_all_caps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/all_caps"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="normal" />
(2)修改strings.xml
添加:
<string name="all_caps">全大写:Welcome to Android World</string>
此时的运行效果:
任务4、实现上标与下标(实现网页的某些效果)
(1)在布局文件里添加两个标签
<TextView
android:id="@+id/tv_theorem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/theorem"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_equation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/equation"
android:textColor="@android:color/holo_green_dark"
android:textSize="20sp" />
(2)修改strings.xml
添加:
<string name="theorem">勾股定理:</string>
<string name="equation">化学方程式:</string>
(3)修改MainActivity
要通过Java代码实现,利用格式转换类Html提供的fromHTML方法来处理含有html标签的字符串。
此时的运行效果:
其实大家还可以尝试其它HTML标签,查看效果。比如样式标签<b>与<i>、标题标签<h1>....<h6>……
任务5:自动链接:网页、邮箱、电话(一般用于设置联系方式)
属性 autoLink,取值:none、web、email、phone、map
在activity_main.xml里添加:
<!-- 分隔线 -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#aaaaaa" />
<TextView
android:id="@+id/tv_web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="web"
android:text="网址:http://www.baidu.com"
android:textColor="@android:color/holo_orange_light"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="email"
android:text="邮箱: maths007@163.com"
android:textColor="@android:color/holo_green_light"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="phone"
android:text="电话: 15892921170"
android:textColor="@android:color/holo_red_light"
android:textSize="15sp" />
此时的运行效果:
比如,单击网址之后的超链接,立马自动访问该网页:
单击电话之后的超链接,自动拨打电话:
任务6、添加有边框和渐变色的文本视图
(1)在res里创建drawable目录,在里面创建custom_border.xml文件。
(2)在布局资源文件里添加一个标签
<!-- 分隔线 -->
<eView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#aaaaaa" />
<TextView
android:id="@+id/tv_bordered_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/custom_border"
android:text="带边框的文本:永不放弃的梦想"
android:textColor="#ff0000"
android:textSize="15sp" />
此时,运行效果:
现在我们来看看在代码里如何设置控件属性。
格式:控件名.set属性名(属性值);
任务7、设置【化学方程式】文本视图的字号与颜色。
此时,运行效果如下:
标签常用属性
1、text:标签文本内容
2、textColor:标签文本颜色(#fff、@color/red)
3、textSize:标签文本字号(20sp)
4、textStyle:标签文本样式(normal | bold | italic)
5、autoLink:自动链接(none、web、email、phone、map)
6、background:背景(背景色、背景图片、背景图形)