android:TextView字段单个字颜色,大小等样式的设定

TextView进一步深化: Textview 可以对其文字进行格式化。
第一类:HTML标签格式化文字
代码比较简单,如下:
 public class AndroidFronColorTest extends Activity {   
      @Override 
      public void onCreate(Bundle savedInstanceState) {   
          super.onCreate(savedInstanceState);   

         setContentView(R.layout.main);   

        TextView htmlFormateTextView = (TextView)findViewById(R.id.testTextView);   

         String source = "这只是一个测试,测试<u>下划线</u>、<i>斜体字</i>、<font color='red'>红色字</font>的格式";   

        htmlFormateTextView.setText(Html.fromHtml(source));   
     }   
 }

第二类通过SpannableString进行格式化操作:
    public TextView mTextView;
    SpannableString styledText;
    public void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_touchevent);
        mTextView = (TextView)findViewById(R.id.spannTextview_id);
        styledText = new SpannableString(mTextView.getText());
        styledText.setSpan(new TextAppearanceSpan(this, R.style.textstyle_blue),0,4,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE );
        styledText.setSpan(new TextAppearanceSpan(this, R.style.textstyle_green),4,8,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE );
        styledText.setSpan(new TextAppearanceSpan(this, R.style.textstyle_gray),8,15,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE );
        mTextView.setText(styledText);
    }
    =======================styles.xml中添加如下
     <!--一个textview中有不同的字体颜色-->
    <style name="textstyle_blue">
        <item name="android:textSize">14dp</item>
        <item name="android:textColor">#4d7bae</item>
    </style>

    <style name="textstyle_gray">
        <item name="android:textSize">17dp</item>
        <item name="android:textColor">#9A9A9A</item>
        <item name="android:textStyle">italic</item>
    </style>

    <style name="textstyle_green">
        <item name="android:textSize">20dp</item>
        <item name="android:textColor">#00ff00</item>
        <item name="android:textStyle">italic</item>
    </style> 
//===================================
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE等的作用:
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值