Android(安卓)TextView 设置多种颜色
16lz
2021-01-26
关键词
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);
更多相关文章
- Flutter -------- BottomNavigationBar 界面切换
- android Material Design Toolbar
- Android信息界面编辑及组合控件的封装
- android一个LinearLayout中具有几个控件,想要使那几个控件在Linea
- Android实现沉浸式(透明)状态栏适配
- Android电子书翻页效果实现
- Android中Dialog自带蒙层适配状态栏
- android沉浸式状态栏的适配(包含刘海屏)
- 关于如何实现android状态栏沉淀式效果