从现在开始,后面会介绍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 滚动控件 之 省市区
  2. Android - 基础控件
  3. GridView属性
  4. android自定义按钮控件样式
  5. Android 控件之ProgressBar进度条
  6. android -------- ConstraintLayout 约束属性(二)
  7. android 悬浮控件-仿360手机助手应用详情页

随机推荐

  1. android 获取md5值 google map key申请
  2. Android Studio菜鸟开发————LinearLa
  3. android的开发 华为手机上不显示menu键
  4. ListView的使用
  5. 建立一个简单的android涂鸦工程
  6. import android eclipse project to andr
  7. android TextView属性汇总
  8. Android 元素居中
  9. Android查看源码
  10. 获取Google Maps API 指纹证书时获取的是