从现在开始,后面会介绍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.

<TextViewandroid: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.

<TextViewandroid: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(){@Overridepublic void onClick(View v){if (!mTvTest.getText().toString().equals("")){showToast(mTvTest.getText().toString());}}});


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


联系方式:ligexiao@gmail.com


更多相关文章

  1. android:EditText属性
  2. GridView属性
  3. EditText的光标选择(android.text.Selection)&输入指定字符
  4. 学习笔记-Android单项选择效果实现
  5. Android静默安装实现
  6. [Android1.6]继承BaseAdapter为GridView设置数据时设置setLayout
  7. android dns设置(无法解析域名问题)
  8. Android(安卓)-- TypedArray
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. COW奶牛!Copy On Write机制了解一下
  2. 2021各公司春招求职上岸大礼包! 春招一次
  3. JavaWeb前端框架之BootStrap基础知识
  4. 3.docker网络模式,桥接模式
  5. 写博客的一些感想和想对读者说的话
  6. 2018年如何快速学Java
  7. DAY1--2021.1.14
  8. 2.docker容器管理、仓库管理、数据管理、
  9. CopyOnWriteArrayList你都不知道,怎么拿of
  10. 从零单排学Redis【铂金一】