android.text.Spannable

 android中android.text包

   1.点亮文字

            TextView tv=(TextView)findViewById(R.id.textView1);  

             String str="thisisatest";
    SpannableStringBuilder style=new SpannableStringBuilder(str); //将str字符串载入SpannableStringBuilder对象中
       style.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);        
       style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
       tv.setText(style);

2.加下划线

            TextView tv=(TextView)findViewById(R.id.textView1);  
String str="thisisatest";
    SpannableStringBuilder style=new SpannableStringBuilder(str); //将str字符串载入SpannableStringBuilder对象中
    CharacterStyle span=new UnderlineSpan();  
       style.setSpan(span, 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);        
       tv.setText(style);

3.插入表情

     TextView tv=(TextView)findViewById(R.id.textView1);  

      Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);  
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
       String str="this is my test"; 
       SpannableString spannable = new SpannableString(str);  
       String rex = " ";  
       Pattern pattern = Pattern.compile(rex);  
       Matcher matcher = pattern.matcher(str);  
       while (matcher.find()) {
        //要让图片替代指定的文字就要用ImageSpan  
       ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);  
       //开始替换
       spannable.setSpan(span, matcher.start(),matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
}
       tv.setText(spannable);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值