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());

    }
  
  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值