標籤:spannablestring android開發 android
SpannableString的屬性:
1、BackgroundColorSpan 背景色
2、ClickableSpan 文本可點擊,有點擊事件
3、ForegroundColorSpan 文本顏色(前景色彩)
4、MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
5、MetricAffectingSpan 父類,一般不用
6、RasterizerSpan 光柵效果
7、StrikethroughSpan 刪除線(中劃線)
8、SuggestionSpan 相當於預留位置
9、UnderlineSpan 底線
10、AbsoluteSizeSpan 絕對大小(文本字型)
11、DynamicDrawableSpan 設定圖片,基於文本基準或底部對齊。
12、ImageSpan 圖片
13、RelativeSizeSpan 相對大小(文本字型)
14、ReplacementSpan 父類,一般不用
15、ScaleXSpan 基於x軸縮放
16、StyleSpan 字型樣式:粗體、斜體等
17、SubscriptSpan 下標(數學公式會用到)
18、SuperscriptSpan 上標(數學公式會用到)
19、TextAppearanceSpan 文本外貌(包括字型、大小、樣式和顏色)
20、TypefaceSpan 文本字型
21、URLSpan 文本超連結
import android.app.Activity;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.text.SpannableString;import android.text.Spanned;import android.text.method.LinkMovementMethod;import android.text.style.BackgroundColorSpan;import android.text.style.ClickableSpan;import android.text.style.ForegroundColorSpan;import android.text.style.StrikethroughSpan;import android.text.style.StyleSpan;import android.text.style.TypefaceSpan;import android.text.style.URLSpan;import android.text.style.UnderlineSpan;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;/* 1、BackgroundColorSpan 背景色 2、ClickableSpan 文本可點擊,有點擊事件 3、ForegroundColorSpan 文本顏色(前景色彩) 4、MaskFilterSpan 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter) 5、MetricAffectingSpan 父類,一般不用 6、RasterizerSpan 光柵效果 7、StrikethroughSpan 刪除線(中劃線) 8、SuggestionSpan 相當於預留位置 9、UnderlineSpan 底線 10、AbsoluteSizeSpan 絕對大小(文本字型) 11、DynamicDrawableSpan 設定圖片,基於文本基準或底部對齊。 12、ImageSpan 圖片 13、RelativeSizeSpan 相對大小(文本字型) 14、ReplacementSpan 父類,一般不用 15、ScaleXSpan 基於x軸縮放 16、StyleSpan 字型樣式:粗體、斜體等 17、SubscriptSpan 下標(數學公式會用到) 18、SuperscriptSpan 上標(數學公式會用到) 19、TextAppearanceSpan 文本外貌(包括字型、大小、樣式和顏色) 20、TypefaceSpan 文本字型 21、URLSpan 文本超連結 */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView textView1, textView2, textView3;textView1 = (TextView) findViewById(R.id.textview1);textView2 = (TextView) findViewById(R.id.textview2);textView3 = (TextView) findViewById(R.id.textview3);String text1, text2, text3;SpannableString spannableString1, spannableString2, spannableString3;text1 = "背景顏色,字型顏色";text2 = "百度連結,字型樣式";text3 = "刪除線 ,底線 ,點擊事件";spannableString1 = new SpannableString(text1);spannableString2 = new SpannableString(text2);spannableString3 = new SpannableString(text3);spannableString1.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);spannableString1.setSpan(new ForegroundColorSpan(Color.RED), 5, 9,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);spannableString2.setSpan(new URLSpan("http://www.baidu.com"), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);spannableString2.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 5, 9,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);spannableString3.setSpan(new StrikethroughSpan(), 0, 3,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);spannableString3.setSpan(new UnderlineSpan(), 5, 8,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);spannableString3.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity.this,activity1.class);startActivity(intent);}}, 9, 14, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);textView1.setText(spannableString1);textView2.setText(spannableString2);textView3.setText(spannableString3);textView1.setMovementMethod(LinkMovementMethod.getInstance());textView2.setMovementMethod(LinkMovementMethod.getInstance());textView3.setMovementMethod(LinkMovementMethod.getInstance());}}
Android---13---SpannableString