第16讲- UI组件之TextView

第16讲 UI组件之TextView

Android系统所有UI类都是建立在View和ViewGroup这两类的基础上的。

所有View的子类称为widget;所有ViewGroup的子类称为Layout。

Android系统借鉴了java中的UI设计思想,包括事件响应机制和布局管理。

UI组件的简单分类(自己定义的):

1. Basic Views --- 常用的View,例如TextView,EditText和Button

2. Pick Views --- 允许用户进行选择的View,例如TimePicker和datePicker

3. List Views --- 显示大量项目的View,例如ListView和spinner

4.DisplayViews --- 显示图片的View,例如Gallery和mageSwitcher

5.Menus --- 显示额外的上下文菜单项目的View

6.Additional Views --- 其他的View,例如AnalogClock和DigitalClock

一、文本视图TextView

显示文本的组件,类似于html当中的label标签。其功能是向用户显示文本内容(不允许编辑),同时可选择性地让用户编辑文本。

组件常见属性:

1. 对宽度影响的属性、

2. 对高度影响的属性、

3.对文本颜色影响的属性、

2. 对文本大小影响的属性、

5. 对文本样式影响的属性、

6.对文本位置影响的属性、

7. 对文本动态性影响的属性(走马灯效果)

1.        对宽度影响的属性有:

android:layout_width//布局宽度,一般是"fill_parent","wrap_content","match_parent"。当然也可以设置数值.

android:width           //设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)。

android:maxWidth

android:minWidth

android:ems          //设置TextView的宽度为N个汉字字符的宽度

android:maxEms      //设置TextView的宽度为最长为N个汉字字符的宽度,与ems同时使用时覆盖ems选项。

android:minEms       //设置TextView的宽度为最短为N个汉字字符的宽度,与ems同时使用时覆盖ems选项。

android:maxLength //限制显示的文本长度,超出部分不显示。

android:singleLine   //设置单行或者多行显示,一旦设置为true,则文字不会自动换行。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。

TextView必须的两个属性:android:layout_width  布局的宽度;  android:layout_height  布局的高度

带"layout"的属性是指整个控件而言的,是与父控件之间的关系,如 layout_gravity 在父控件中的对齐方式, layout_margin 是级别相同的控件之间的间隙等等;

不带"layout" 的属性是指控件中文本的格式,如gravity是指文本的对齐方式等等,而其中文本的格式又受制约于它的控件在父控件中的属性.

长宽相对设置

android:layout_width = "fill_parent" //设置为组件宽度/高度与父窗口的一致(其他属性不能再影响宽度和高度了)

android:layout_height = “wrap_content” <!--组件长/宽随着内容变化 -->

android:background="#00ff00"

android:width="100dp"            <!--此时不再起作用,因为有fill_parent-->

android:ems="2"    //设置TextView的宽度为N个固定的汉字字符的宽度,2即为2个汉字宽度,4个字符。

android:singleLine="true" //设置单行或者多行显示,一旦设置为true,则文字不会自动换行。

2.        对高度影响的属性有:

android:layout_height//布局高度,一般是"fill_parent","wrap_content","match_parent"。当然也可以设置数值.

android: heigh            //设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)

android:maxHeight

android:minHeight

android:lines                      //设置文本的行数,若设置两行就显示两行,即使第二行没有数据。

android:maxLines             //设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。

android:minLines                  //设置文本的最小行数,与lines类似

android:lineSpacingExtra         //设置行间距

android:singleLine

3.        对颜色影响的属性有:

android:textColor                             //设置文本颜色

android:textColorHighlight        //被选中文字的底色,默认为蓝色

例如:android:textColor="#00ffff"

android:textColorHighlight="#cccccc"

4.        对文字大小影响的属性有:

android:textSize="15sp"                         //设置文字大小,推荐度量单位”sp”,如”15sp”

5.        对文本字体样式的设置:

android:textStyle="normal"             //设置字形[bold(粗体) 0,italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开

android:typeface               //设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]

6.        对文本显示位置的设置:

android:gravity="center"         //设置文本在文本组件里面的位置,如果设成“center”,文本居中显示。

7.        对文本动态性影响的属性(走马灯效果)

所谓跑马灯效果就是当文字超过控件所能容纳的空间时,在控件内滚动的效果。

Android系统中TextView实现跑马灯效果,必须具备以下几个条件:

1、android:ellipsize=”marquee” ,设置为滚动效果

2、android:singleLine,TextView必须单行显示,即内容必须超出TextView大小

3、android:focusable ,TextView要获得焦点才能滚动

<TextView

android:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:singleLine="true"       //当singlLine时,默认为true。必须单行显示,即内容必须超出TextView大小

android:ellipsize="marquee"  //设置为滚动效果,以横向滚动方式显示

android:marqueeRepeatLimit="marquee_forever"        //设置走马灯滚动次数,无限循环

android:focusable="true"                                             //获得焦点,TextView要获得焦点才能滚动

android:focusableInTouchMode="true"                 //设置在Touch模式下View是否能取得焦点

android:text="hello world my dearfriend this is a long long string,  longlong string" />