Textview在平常開發工做中使用很是頻繁,在這裏簡單總結一下其比較重要的用法。目標是設置textview的中一段文字的顯示屬性,例如指定文字要下劃線顯示(@XXX、#XXX#),文字替換成圖片,文字加劇、變色、變大等。
實現的基本思路都是將指定的文字找出來,而後用實現Spananble接口的子類覆蓋,便可。
Spananble只是一個接口,它的繼承關係是:Spananble->Spanned->CharSequence
由於Spannable繼承於CharSequence接口,於是,TextView能夠直接setText,Spannable的實現類:Editable, SpannableString, SpannableStringBuilder 。
Spannable中有提供public abstract void setSpan (Object what, int start, int end, int flags) 方法,來設置被替換部分要顯示的樣式
其中參數Object what 又必須是 android.text.style.CharacterStyle 的實現子類,就是它決定顯示的樣式,例如實現下劃線的UrlSpan,實現改變字體顏色的ForegroundColorSpan,實現改變字體大小的AbsoluteSizeSpan,實現替換成圖片的ImageSpan等。下面我會總結幾種經常使用的用法:
一、改變文字的大小以及顏色: String str = "This is testing text!"
int start =str.indexOf("testing");
int end = start + "testing ".length();
SpannableStringBuilder style = new SpannableStringBuilder(str);
int size = 20; //20px
style.setSpan(new AbsoluteSizeSpan(size), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int color = 0xff0000; //設置指定位置文字的顏色
style.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
...
textView.setText(style);
二、實現多字符串的動態處理:
string.xml:
<string name="testing">這是一個數字:%1$d, 這是兩位數字:%2$d,這又是成都爲3的字符串:%3$s</string>
String text = String.format(getResources().getString(R.string.testing), 1, 22, "三個字");
int index[] = new int[3];
index[0] = text.indexOf("1");
index[1] = text.indexOf("22");
index[2] = text.indexOf("三個字");
SpannableStringBuilder style=new SpannableStringBuilder(text);
style.setSpan(new ForegroundColorSpan(Color.RED),index[0],index[0]+1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.RED),index[1],index[1]+2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
style.setSpan(new BackgroundColorSpan(Color.RED),index[2],index[2]+3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(style);
在項目開發中,咱們常常須要把多個字符串進行拼接,用上面的方式處理,並結合起來使用,能夠避免不少Textview的拼接!
三、實現超連接效果:
第一種簡單作法:當文字中出現URL、E-mail、電話號碼等的時候,能夠將TextView的android:autoLink屬性設置爲相應的的值,也能夠是全部的類型都都響應:android:autoLink="all",最後在java代碼裏,對指定的textView.setAutoLinkMask(Linkify.ALL);
第二種自定義作法:儘管第一種作法簡單,但都是默認調用,不能自定義跳轉,像微博的@XXX ,#XXX#這類的特殊文字的響應,須要指定處理的視圖。咱們通常能夠這樣子實現:
(1)咱們先指定特定字符串響應點擊:
SpannableStringBuilder style = new SpannableStringBuilder("call: 110");
style.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new URLSpan("110"), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(style);
textView.setMovementMethod(LinkMovementMethod.getInstance());
(2)更改響應點擊的方式:
URLSpan是咱們經常使用的文字超連接控件,可是默認都會自帶下劃線,並非太美觀。有官方API文檔可知,URLSpan繼承自
ClickableSpan
,
於是咱們能夠經過繼承
ClickableSpan
並重寫其實現的方法,便可實現自定義跳轉控制,以及外觀樣式的控件了。
public class URLSpanNoUnderline extends ClickableSpan {
private final String mURL;
public URLSpanNoUnderline(String url) {
mURL = url;
}
public String getURL() {
return mURL;
}
@Override
public void onClick(View widget) {
Uri uri = (Uri) widget.getTag();
if(uri!=null) {
final Context context = widget.getContext();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
context.startActivity(intent);
}
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false); //取消下劃線
ds.setColor(0xff0066ff); //指定文字顏色
//ds.setTextSize(ds.getTextSize()*1.1F);
}
}
(3)實現響應:
咱們經過設置Intent的Action----
Intent.ACTION_VIEW,並指定攜帶的Data是uri,那麼咱們就能夠在程序中,實現響應該Action和Data意圖的Activity:
<activity
android:name=".activity.XXXActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="_host"
android:scheme="_scheme" />
</intent-filter>
</activity>
(4)最後是自定義Uri
uri通常由scheme和host,就像http的url同樣,http://www.google.com,其scheme是http,host就是www.google.com,兩者結合就可定位到某一位置。uri也同樣,固然帶參數也是能夠的,例如:my_scheme://my_host?param=www.google.com。
總結一下,經過重寫實現
ClickableSpan
接口,並自定義Uri,便可使其
響應
導向指定的activity進行。
未完待續...
本文由zhiweiofli編輯發佈,轉載請註明出處,謝謝。