关键词

Android 、TextView、多种颜色

摘要

由于项目开发需要,一个 TextView 为了强调内容,需要显示不同的字体颜色和大小

效果图

TextView效果图.png

方法一

        int totalCount = 3;        double totalPrice = 33.8;        String str = "共 " + totalCount + " 件商品,"                + "已付款 ¥" + String.format("%.2f", totalPrice) + " 元";        Spanned content =  Html.fromHtml(str);        mTextView.setText (content);

注意:不能将包含 Html 标签的字符串放到 strings.xml

方法二

        int totalCount = 3;        double totalPrice = 33.8;        String totalCountStr = totalCount +"";        String totalPriceStr = String.format("%.2f", totalPrice);       String str = "共 " + totalCountStr + "件商品,"                + "已付款¥" + totalPriceStr  + "元";       SpannableStringBuilder style = new SpannableStringBuilder(str);         style.setSpan(new ForegroundColorSpan(Color.RED), 1, (totalCountStr +1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);         style.setSpan(new ForegroundColorSpan(Color.RED), (totalCountStr+9), (str.length()-1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);         mTextView.setText(style);  

更多相关文章

  1. Flutter -------- BottomNavigationBar 界面切换
  2. android Material Design Toolbar
  3. Android信息界面编辑及组合控件的封装
  4. android一个LinearLayout中具有几个控件,想要使那几个控件在Linea
  5. Android实现沉浸式(透明)状态栏适配
  6. Android电子书翻页效果实现
  7. Android中Dialog自带蒙层适配状态栏
  8. android沉浸式状态栏的适配(包含刘海屏)
  9. 关于如何实现android状态栏沉淀式效果

随机推荐

  1. Android:推荐学习
  2. ch031 Android AsyncTask
  3. ch016 Android 自定义对话框.
  4. Android 垂直的Slidebar 代码
  5. ch028 Android 断点续传
  6. My Android Camera Notes
  7. Android:GridView+AbsoluteLayout作一个
  8. Android 数据库技术
  9. android ImageView scaleType属性
  10. RadioButton修改标志图片