2012年8月18日 星期六

Spannable String 的變化


Spannable String 的變化

public class Cq_SpannableStringActivity extends Activity {
    /** Called when the activity is first created. */
TextView mtv = null;
SpannableString msp = null;
String source = "字體測試字體大小一半兩倍前景色背景色正常粗體斜體粗斜體下畫線刪除線x1x2電話郵件網站斷信採信地圖X軸綜合/bot";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mtv = (TextView) findViewById(R.id.textview);
        
        //Create a SpannableString obj
        msp = new SpannableString(source);
        
        //Set font (default, default-bold, monospace, serif, san-serif)
        msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new TypefaceSpan("serif"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        //Set font Size (絕對值,單位:像素)
        msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new AbsoluteSizeSpan(20, true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //dip true: 表示字體大小單位為dip,否則就是像素,同上
        
      //Set font Size (相對值,單位:像素) 參數表示為默認字體大小的倍數
        msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new RelativeSizeSpan(2.5f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        //Set foreground color
        msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
      //Set Background color
        msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        //Set fontstyle:regular, bold, italic, bold-italic
        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);
        
        //Set underline, strikethrough, subscript, superscript
        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);
        
        //Links (setMovementMethod  should be added)
        msp.setSpan(new URLSpan("tel:0229205319"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new URLSpan("mailto:cqchoucq@gmail.com"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new URLSpan("http://tw.yahoo.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new URLSpan("sms:0229205319"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new URLSpan("mms:0229205319"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new URLSpan("geo:38.899533, -77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        //Set fontSize (相對值,單位:像素)參數表示為默認字體寬度的多少倍
        msp.setSpan(new ScaleXSpan(2.0f), 49, 51, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //2.0f 為寬度的兩倍,即x軸方向放大默認字體的兩倍,而高度不變
        
        
        //Set font (including font name, style, color, linking color)
        ColorStateList csllink = null;
        ColorStateList csl = null;
        
        XmlResourceParser xppcolor = getResources().getXml(R.color.color);
        try {
        csl = ColorStateList.createFromXml(getResources(), xppcolor);
        }catch(XmlPullParserException e) {
        e.printStackTrace();
        }catch(IOException e) {
        e.printStackTrace();
        }
        
        XmlResourceParser xpplinkcolor = getResources().getXml(R.color.linkcolor);
        try {
        csllink = ColorStateList.createFromXml(getResources(), xpplinkcolor);
        }catch(XmlPullParserException e) {
        e.printStackTrace();
        }catch(IOException e) {
        e.printStackTrace();
        }
        
        msp.setSpan(new TextAppearanceSpan("monospace", android.graphics.Typeface.BOLD_ITALIC, 30, csl, csllink), 51, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        
        //Set list item symbol
        msp.setSpan (new BulletSpan(android.text.style.BulletSpan.STANDARD_GAP_WIDTH, Color.GREEN), 0, msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //第一個參數表示symbol的寬度,第二個參數表示symbol的顏色
        
        //setPicture
        Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        msp.setSpan(new ImageSpan(drawable), 53, 57, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        mtv.setText(msp);
        mtv.setMovementMethod(LinkMovementMethod.getInstance());
        
    }
}

沒有留言:

張貼留言