Android一个TextView显示不同样式的文本

一个TextView有不同样式这里用的SpannableString来实现。如下图所示的效果:

Paste_Image.png

上面的效果需要设置两个样式

<style name="money_style1">
        <item name="android:textSize">12sp</item>
        <item name="android:textStyle">normal</item>//正常字体
    </style>
    <style name="money_style2">
        <item name="android:textSize">15sp</item>
        <item name="android:textStyle">bold</item>//加粗
    </style>
  String price=vh.price.getText().toString();
        int len=price.length();
        SpannableString spannableString=new SpannableString(price);
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.money_style1),0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.money_style2),1,price.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new TextAppearanceSpan(context,R.style.money_style1),price.length()-1,price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        vh.price.setText(spannableString);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容