在android中,textview是我們最常用的用來顯示文本的控件。
一般情況下,textview中的文本都是一個樣式。那麼如何對于textview中各個部分的文本來設定字型,大小,顔色,樣式,以及超級連結等屬性呢?下面我們通過spannablestring的具體執行個體操作來示範一下。
res-layout-main.xml
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
<textview
android:id="@+id/mytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</linearlayout>
res-color-color.xml
res-color-linkcolor.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffffff00"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff00ffff"/> <!-- focused -->
<item android:color="#ff0ff000"/> <!-- default -->
</selector>
textviewlinkactivity
package com.snowdream;
import java.io.ioexception;
import org.xmlpull.v1.xmlpullparserexception;
import android.app.activity;
import android.content.res.colorstatelist;
import android.content.res.xmlresourceparser;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.color;
import android.graphics.drawable.drawable;
import android.os.bundle;
import android.text.spannablestring;
import android.text.spanned;
import android.text.method.linkmovementmethod;
import android.text.style.absolutesizespan;
import android.text.style.backgroundcolorspan;
import android.text.style.bulletspan;
import android.text.style.drawablemarginspan;
import android.text.style.foregroundcolorspan;
import android.text.style.iconmarginspan;
import android.text.style.imagespan;
import android.text.style.relativesizespan;
import android.text.style.scalexspan;
import android.text.style.strikethroughspan;
import android.text.style.stylespan;
import android.text.style.subscriptspan;
import android.text.style.superscriptspan;
import android.text.style.textappearancespan;
import android.text.style.typefacespan;
import android.text.style.urlspan;
import android.text.style.underlinespan;
import android.widget.textview;
public class textviewlinkactivity extends activity {
textview mtextview = null;
spannablestring msp = null;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
mtextview = (textview)findviewbyid(r.id.mytextview);
//建立一個 spannablestring對象
msp = new spannablestring("字型測試字型大小一半兩倍前景色背景色正常粗體斜體粗斜體下劃線删除線x1x2電話郵件網站短信彩信地圖x軸綜合/bot");
//設定字型(default,default-bold,monospace,serif,sans-serif)
msp.setspan(new typefacespan("monospace"), 0, 2, spanned.span_exclusive_exclusive);
msp.setspan(new typefacespan("serif"), 2, 4, spanned.span_exclusive_exclusive);
//設定字型大小(絕對值,機關:像素)
msp.setspan(new absolutesizespan(20), 4, 6, spanned.span_exclusive_exclusive);
msp.setspan(new absolutesizespan(20,true), 6, 8, spanned.span_exclusive_exclusive); //第二個參數boolean dip,如果為true,表示前面的字型大小機關為dip,否則為像素,同上。
//設定字型大小(相對值,機關:像素) 參數表示為預設字型大小的多少倍
msp.setspan(new relativesizespan(0.5f), 8, 10, spanned.span_exclusive_exclusive); //0.5f表示預設字型大小的一半
msp.setspan(new relativesizespan(2.0f), 10, 12, spanned.span_exclusive_exclusive); //2.0f表示預設字型大小的兩倍
//設定字型前景色
msp.setspan(new foregroundcolorspan(color.magenta), 12, 15, spanned.span_exclusive_exclusive); //設定前景色為洋紅色
//設定字型背景色
msp.setspan(new backgroundcolorspan(color.cyan), 15, 18, spanned.span_exclusive_exclusive); //設定背景色為青色
//設定字型樣式正常,粗體,斜體,粗斜體
msp.setspan(new stylespan(android.graphics.typeface.normal), 18, 20, spanned.span_exclusive_exclusive); //正常
msp.setspan(new stylespan(android.graphics.typeface.bold), 20, 22, spanned.span_exclusive_exclusive); //粗體
msp.setspan(new stylespan(android.graphics.typeface.italic), 22, 24, spanned.span_exclusive_exclusive); //斜體
msp.setspan(new stylespan(android.graphics.typeface.bold_italic), 24, 27, spanned.span_exclusive_exclusive); //粗斜體
//設定下劃線
msp.setspan(new underlinespan(), 27, 30, spanned.span_exclusive_exclusive);
//設定删除線
msp.setspan(new strikethroughspan(), 30, 33, spanned.span_exclusive_exclusive);
//設定上下标
msp.setspan(new subscriptspan(), 34, 35, spanned.span_exclusive_exclusive); //下标
msp.setspan(new superscriptspan(), 36, 37, spanned.span_exclusive_exclusive); //上标
//超級連結(需要添加setmovementmethod方法附加響應)
msp.setspan(new urlspan("tel:4155551212"), 37, 39, spanned.span_exclusive_exclusive); //電話
msp.setspan(new urlspan("mailto:[email protected]"), 39, 41, spanned.span_exclusive_exclusive); //郵件
msp.setspan(new urlspan("http://www.baidu.com"), 41, 43, spanned.span_exclusive_exclusive); //網絡
msp.setspan(new urlspan("sms:4155551212"), 43, 45, spanned.span_exclusive_exclusive); //短信 使用sms:或者smsto:
msp.setspan(new urlspan("mms:4155551212"), 45, 47, spanned.span_exclusive_exclusive); //彩信 使用mms:或者mmsto:
msp.setspan(new urlspan("geo:38.899533,-77.036476"), 47, 49, spanned.span_exclusive_exclusive); //地圖
//設定字型大小(相對值,機關:像素) 參數表示為預設字型寬度的多少倍
msp.setspan(new scalexspan(2.0f), 49, 51, spanned.span_exclusive_exclusive); //2.0f表示預設字型寬度的兩倍,即x軸方向放大為預設字型的兩倍,而高度不變
//設定字型(依次包括字型名稱,字型大小,字型樣式,字型顔色,連結顔色)
colorstatelist csllink = null;
colorstatelist csl = null;
xmlresourceparser xppcolor=getresources().getxml (r.color.color);
try {
csl= colorstatelist.createfromxml(getresources(),xppcolor);
}catch(xmlpullparserexception e){
// todo: handle exception
e.printstacktrace();
}catch(ioexception e){
}
xmlresourceparser xpplinkcolor=getresources().getxml(r.color.linkcolor);
csllink= colorstatelist.createfromxml(getresources(),xpplinkcolor);
msp.setspan(new textappearancespan("monospace",android.graphics.typeface.bold_italic, 30, csl, csllink), 51, 53, spanned.span_exclusive_exclusive);
//設定項目符号
msp.setspan(new bulletspan(android.text.style.bulletspan.standard_gap_width,color.green), 0 ,msp.length(), spanned.span_exclusive_exclusive); //第一個參數表示項目符号占用的寬度,第二個參數為項目符号的顔色
//設定圖檔
drawable drawable = getresources().getdrawable(r.drawable.icon);
drawable.setbounds(0, 0, drawable.getintrinsicwidth(), drawable.getintrinsicheight());
msp.setspan(new imagespan(drawable), 53, 57, spanned.span_exclusive_exclusive);
mtextview.settext(msp);
mtextview.setmovementmethod(linkmovementmethod.getinstance());
}
}
效果預覽: