SuperbookKing

3.用户接口UI布局----View控件的概述之TextView

TextView(view 子类)----控件能向用户展现文本信息

// 在程序中创建TextView对象

TextViewtv=new TextView(this);

tv.setText("你好");

setContentView(tv);

// 在XML布局文件中使用----通过解析xml文件构造view类

设置字体的大小推荐使用sp作为单位

android:layout_width="wrap_content"
android:layout_height="fill_parent"

android:height设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_height 的区别看这里。

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

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

android:textColor设置文本颜色
android:textColorHighlight被选中文字的底色,默认为蓝色
android:textColorHint设置提示信息文字的颜色,默认为灰色。与hint一起使用。
android:textColorLink文字链接的颜色.

android:gravity设置文本位置,如设置成“center”,文本将居中显示。
android:ellipsize设置当文字过长时,该控件该如何显示。

android:drawableLeft在text的左边输出一个drawable,如图片。
android:drawablePadding设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。

注:

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

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

     dp也就是dip。这个和sp基本类似。如果设置表示长度、高度等属性时可以使用dpsp但如果设置字体,需要使用spdp是与密度无关,sp除了与密度无关外,还与scale无关。如果屏幕密度为160,这时dp和sp和px是一样的。1dp=1sp=1px,但如果使用px作单位,如果屏幕大小不变(假设还是3.2寸),而屏幕密度变成了320。那么原来TextView的宽度设成160px,在密度为3203.2寸屏幕里看要比在密度为1603.2寸屏幕上看短了一半。但如果设置成160dp160sp的话。系统会自动将width属性值设置成320px的。也就是160 * 320 / 160。其中320 / 160可称为密度比例因子。也就是说,如果使用dpsp,系统会根据屏幕密度的变化自动进行转换。 

<TextView
android:id="@+id/tv1"
android:gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#00FF00"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/textView1"
android:textSize="20sp" />//跑马灯

2.EditView

 

posted on 2012-07-21 14:00  SuperbookKing  阅读(228)  评论(0)    收藏  举报