在这里介绍几个比较重要与常用的TextView属性。
android:autoLink:设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)。
举例:
在string.xml中定义以下键值对:
<string name="hello">Hello World,TextViewTestActivity!</string>
<string name="app_name">TextViewTest</string>
<string name="vebUrl">www.hao123.com</string>
<string name="email">396366184@qq.com</string>
<string name="phoneNumber">电话号码: 15900001234</string>
<string name="autoAll">电话号码:15900001234 网址:www.hao123.com 邮箱:396366184@qq.com sword</string>
在autolink_layout中:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/vebUrl" android:autoLink="web" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/email" android:autoLink="email" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/phoneNumber" android:autoLink="phone" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/mapUrl" android:autoLink="map" /> <TextView android:id="@+id/tvHtml" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="all" />
在虚拟器上效果:
android:text 设置显示文本
android:textStyle 设置文本字体类型
android:textColor 设置文本字体颜色
android:background 设置背景颜色
android:singleLine 当值为true时设置单行显示,和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。
android:ellipsize 设置当文字过长时,该控件该如何显示,值为start—省略号显示在开头,end—省略号显示在结尾,midle省略号显示在中间,marquee以跑马灯的方式显示(动画横向移动),以跑马灯的方式显示还需配合其它几个属性:
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true" 下面的例子中会有实例
举例分别介绍几个属性:
在font.xml中:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="正常的文本内容" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#ff0000"
android:text="倾斜的带颜色的字体" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:textStyle="bold"
android:textSize="30dp"
android:text="加粗,背景颜色,字体30dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="只显示一行字符串超出屏幕为'...'dsfusdiofjdsiofjsdiofjoisdjfiosdjfoisdjfoisdf"
android:singleLine="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textScaleX="0.5"
android:textColor="#ff0000"
android:text="红色字体设置显示文字的间隔为0.5" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textScaleX="2.0"
android:textColor="#ff0000"
android:text="红色字体设置显示文字的间隔为2.0" />
<TextView
android:layout_width="200px"
android:layout_height="wrap_content"
android:textSize="20dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:text="文字滚屏文字跑马灯效果文字滚屏文字跑马灯效果" />
<cn.csdn.activity.BorderTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="带边框的文字"/>
</LinearLayout>
绘制带边框的文字在cn.csdn.activity.BorderTextView中:
package cn.sword.activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyBorderTextView extendsTextView {
//必须覆盖带两个参数的构造
publicMyBorderTextView(Context context,AttributeSet attrs) {
super(context,attrs);
}
//覆盖父类的onDraw()方法
publicvoid onDraw(Canvas canvas){
super.onDraw(canvas);
//创建画刷(画笔)
Paintpaint = new Paint();
//设置绘画颜色
paint.setColor(Color.GREEN);
//画水平第一条线
canvas.drawLine(0,0, this.getWidth()-1, 0, paint);
//画垂直第一条线
canvas.drawLine(0,0, 0, this.getHeight()-1, paint);
//画水平第二条线
canvas.drawLine(this.getWidth()-1,0, this.getWidth()-1, this.getHeight()-1, paint);
//画垂直第二条线
canvas.drawLine(0,this.getHeight()-1 , this.getWidth()-1, this.getHeight()-1, paint);
}
}
在模拟器中的显示效果: