Android碎碎念 -- 设置文字颜色

本文深入探讨了为何大部分程序员难以晋升为架构师的原因,通过实例解析JAVA代码使用工具Html.fromHtml、字符串处理Spannable及使用颜色值从XML资源的实践技巧,并通过WebView展示代码应用效果。
1.JAVA代码使用工具Html.fromHtml:
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
2.字符串处理Spannable:

TextView text = (TextView) findViewById(R.id.text_login);
text.setText("");
text.append("Add all your funky text in here");
Spannable text = (Spannable) text.getText();
text.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);

3.使用颜色值从XML资源:
int сolor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2);
Html.fromHtml(String.format("<font color=\"#\">text</font>", сolorString), TextView.BufferType.SPANNABLE);
4.使用的WebView:
WebView webview = new WebView(this);
String summary = "<html><body>Sorry, <span style=\"background:red;\">Madonna</span> gave no results</body></html>";
webview.loadData(summary, "text/html", "utf-8");

转载于:https://my.oschina.net/miger/blog/534131

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值