Android碎碎念 -- 设置文字颜色
16lz
2021-01-23
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");
更多相关文章
- Android TextView 个别文字字体颜色格式
- 点击listitem按下效果替换默认颜色
- Android 字符串转换大小写
- android 计算字符串长度,高度
- Android中五大字符串总结(String、StringBuffer、StringBuilder、
- Android状态栏颜色修改
- EditText 各部分颜色设置小结