Android程序的根本控件使用之文本显示组件:TextView
Android程序的基本控件使用之文本显示组件:TextView
文本显示组件:TextView
掌握文本显示组件的配置及使用
掌握文本显示组件的继承结构
掌握文本显示组件的基本属性及操作方法
内容:
对于文本组件而言主要的目的是静态的显示一些文字,就想到于完成了一些标签的显示功能。
Android.Widget. TextView类时View类的直接子类,所以在本组件之中也会提供更多的操作方法及配置的相关属性:
Java.Lang.Object
Android.View.View
Android.Widget.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" > <TextView 定义文本组件 android:id="@+id/mytext1" 定义组件ID android:layout_width="fill_parent" 组件宽度为屏幕宽度 android:layout_height="wrap_content" 组件高度为文字高度 android:textColor="#FFFF00" //字体设置黄色 android:textSize="12px" //字的大小为12像素 android:text="广西你好!" 设置显示文字 /> </LinearLayout>
|
对于文本的大小必须有一个大小的单位,而px表示像素。
Px(pixels):像素
Dip(decice independent pixels):依赖于设备的像素
Sp(scaled pixels—best for text size):带比例的像素
Pt(points):点
In(inches):英尺
Mm(millimeters):毫米’’
<TextView android:id="@+id/mytext2" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_margin="30px" 表示距离上下有30个像素 android:text="网址:www.baidu.com" /> |
<TextView android:id="@+id/mytext2" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="10px" android:text="徐辉"
/> |
<TextView android:id="@+id/mytext2" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="10px" 距离上面有10个像素 android:text="徐辉" 默认显示文字 android:maxLength="1" 最多只显示1个文字 /> |
<TextView android:id="@+id/mytext4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/xuhui" android:textColor="#FFFF00" android:text="这是在背景上的信息徐辉"
/> |
<TextView android:id="@+id/mytext4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/xuhui" android:textColor="#000000" 黑色字体 android:textStyle="bold" 字体改为粗体 android:text="这是在背景上的信息徐辉"
/> |
这些就是在文本显示上的一些变化,而对于文本的显示风格有以下几种
设置文字的显示风格(android:textStyle):
正常(normal)粗体(bold)倾斜(italic)
所有组件在R.java中生成(我这没有)
以上只是进行了一些基本的文本信息的提示功能的实现,在Android之中,文本的功能不止如此,在Android之中如果文本显示组件上出现了一些网址信息也可以将其变为链接的形式出现。
<TextView android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="all" 里面的链接内容自动变为地址链接 android:textColor="#FFFF00" android:textSize="45px" android:text="网址: www.baidu.com" /> |
了解了基本使用之后,现在也会出现一个问题,如果说要在一个项目之中定义多个文本组件,那么这可能会存在着许多重复的配置属性,这样的话对于开发维护很麻烦,所以在Android操作系统之中也可以使用样式表文件进行统一的属性配置。
范例:定义一个样式表—styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="msg_style"> 定义样式表的配置,其中“msg_style”为名字 <item name="android:textSize">45px</item> 定义文本大小 <item name="android:textColor">#FFFF00</item> 定义文本颜色 <item name="android:autoLink">all</item> 定义链接显示文字 <item name="android:layout_height">wrap_content</item> 组件的高度 <item name="android:layout_width">fill_parent</item> 组件的宽度
</style>
</resources> |
如果要想引用样式表文件
在main中
<?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/msg" 组件ID style="@style/msg_style" 定义组件显示的样式风格 android:text="网址: www.baidu.com" />
</LinearLayout>
|
通过此种方式的显示更加方便于系统的维护操作,对于文本显示真正使用的就是标签的提示信息。
总结:
TextView作为文本组件主要的功能是显示文本数据;
所有的组件可以直接通过一个样式表文件进行属性的配置