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.

<TextView android:id="@+id/tv_marquee" android:layout_width="fill_parent" android:layout_height="wrap_content" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:focusable="true" android:ellipsize="marquee" android:singleLine="true" android:textColor="#fff00f" android:text="@string/ellipsize" />

(3)设置链接和下划线

android:autoLink="all"

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

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

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


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

设置链接的文本的颜色


eg.

<TextView android:id="@+id/tv_autolink" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ellipsize="middle" android:autoLink="all" android:textColorLink="@android:color/secondary_text_light" android:singleLine="true" android:text="@string/auto_link" />

(4)一般文本加下划线

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

eg.

<string name="underline"><u>phone: 1390123456</u></string>


或者在代码实现:

textView.setText(Html.fromHtml("<u>"+"hahaha"+"</u>"));

(5)在文本中增加图片

android:drawableLeft="@drawable/ic_launcher"

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

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


eg.

<TextView android:id="@+id/tv_draw" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ellipsize="middle" android:drawableLeft="@drawable/ic_launcher" android:singleLine="true" android:text="@string/draw_left" />


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

TextView mTvTest = (TextView)findViewById(R.id.tv_draw); 通过 findViewById函数就可以获得相应id的TextView控件


(7)点击事件

通过setOnClickListener函数实现;

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

mTvTest.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { if (!mTvTest.getText().toString().equals("")) { showToast(mTvTest.getText().toString()); } } });


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


联系方式:ligexiao@gmail.com


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值