FontStyleTestActivity

package hyz.com.font;

import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.ComponentName;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;

public class FontStyleTestActivity extends Activity
{
    String strs="我的心太亂了,給我點空白。";   
    TextView tv,tv1,tv2,tv3,tv4,tv5;    
    int start =3;       
    int end = 5;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        tv=(TextView)findViewById(R.id.tv);
        tv1=(TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        tv3=(TextView)findViewById(R.id.tv3);
        tv4=(TextView)findViewById(R.id.tv4);
        tv5=(TextView)findViewById(R.id.tv5);
        
        method1();
        method2();
        method3();
        method4();
        method5();
    }
 //設置部分文字背景高亮顯示:
    private void method1()
    {
    
        SpannableStringBuilder style=new SpannableStringBuilder(strs);      
        style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
        tv1.setText(style); 
    }
    //同時設置文字和背景高亮顯示:
    private void method2()
    {
     SpannableStringBuilder style=new SpannableStringBuilder(strs);   
     style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  
     tv2.setText(style);  
    }
    private void method3()
    {
        SpannableStringBuilder style=new SpannableStringBuilder(strs);      
        style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
        tv3.setText(style); 
    }
    /*
     * Spannable.SPAN_EXCLUSIVE_EXCLUSIVE :
     * 即在原文本頭或尾追加新文本的樣式不受原文本樣式影響,原文本高亮,新追加文本不高亮
     * Spannable.SPAN_EXCLUSIVE_INCLUSIVE :
     * 即在原文本尾追加新文本的樣式受原文本樣式影響,原來文本尾高亮,新追加文本也高亮

     */
    private void method4()
    {
     tv4.setText(Html.fromHtml("Your big island <b>ADVENTURE!</b>"));
      
    }
    private void method5()
    {
     SpannableString ss = new SpannableString( "紅色打電話粗體刪除線綠色下劃線圖片:.");
     SpannableString ss1 = new SpannableString( "紅色打電話粗體刪除線綠色下劃線圖片:.");
        ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);       
        ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //粗斜體
        ss.setSpan(new StyleSpan(Typeface.BOLD), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new StrikethroughSpan(), 7, 10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new UnderlineSpan(), 10, 16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.grey)), 10, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
//        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//        ss.setSpan(span, 18, 19, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        ss.setSpan(new AbsoluteSizeSpan(18), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        Map<String , Object> map = new HashMap<String , Object>();
     map.put("key", ss);
        tv5.setText((SpannableString)map.get("key"));       
        tv5.setMovementMethod(LinkMovementMethod.getInstance());

    }
  
  
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章