`
kanwoerzi
  • 浏览: 1648032 次
文章分类
社区版块
存档分类
最新评论

Android控件开发之一----TextView

 
阅读更多
从现在开始,后面会介绍android中的一些常用的控件。。。

今天介绍TextView这个控件,这也是android的HelloWorld中第一个不用自己写代码,就自动生成的。。。


TexitView是向用户展示文字的(也就是用户界面上能看到的文字),以下是开发文档上的原文:

Displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; see EditText for a subclass that configures the text view for editing.


介绍下TextView的属性:

(1)设置字体的一些属性

android:textColor="#fff00f"--------------设置字体的颜色
android:textSize="30sp"---------------设置字体的大小(单位一般应sp)

android:textStyle="bold"---------------设置字体的属性(粗体,斜体等)

android:typeface="sans"--------------设置字体(sans等)


(2)走马灯效果实现

实现走马灯效果必须注意,TextView一定要获取焦点

以下属性合起来一起,才能实现走马灯的效果,却以不可

android:marqueeRepeatLimit="marquee_forever"(注1)
android:focusableInTouchMode="true"
android:focusable="true"
android:ellipsize="marquee" (注2)
android:singleLine="true"

这五个缺一不可.。。。


注1

android:marqueeRepeatLimit="marquee_forever"

这个属性的值,有三种方式:

a.默认就是循环3次;b.marquee_forever无限循环;c.自己赋值(如1,2,3等);


注2

android:ellipsize="marquee"

这个属性的作用就是只显示一行,但是内容太多又显示不下,则用省略号等方式,其值分别为marquee(走马灯),start,middle,end;

这属性要和android:singleLine="true"一起使用,且文字的长度要大于显示的宽度;

eg.


(3)设置链接和下划线

android:autoLink="all"

设置文本中一些特殊的值加下划线(如手机号码,网址,email,map等),

他得值就是phone,map,email,web,all(包括全部);

点击的时候,会相应链接到各自的应用属性;


android:textColorLink="@android:color/secondary_text_light"

设置链接的文本的颜色


eg.


(4)一般文本加下划线

只要在string.xml的字段中进行修改就可以了

eg.


或者在代码实现:

(5)在文本中增加图片

android:drawableLeft="@drawable/ic_launcher"

设置图片的位置在文本的左边

android:drawableTop="@drawable/ic_launcher"---------上
android:drawableDown="@drawable/ic_launcher"-----------下
android:drawableRight="@drawable/ic_launcher"-----------右


eg.


(6)代码中如何调用控件

通过findViewById函数就可以获得相应id的TextView控件


(7)点击事件

通过setOnClickListener函数实现;

参数是一个OnClickListener的Interface,实现它,在onClick函数中处理要处理的event


还有其他一些属性,请参考博文xml属性大剖析


联系方式:ligexiao@gmail.com


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics