一:TextView组件改变部分文字的颜色:
- TextView textView = (TextView)findViewById(R.id.textview);
- //方法一:
- textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));
- //方法二:
- String text = "获得银宝箱!";
- SpannableStringBuilder style=new SpannableStringBuilder(text);
- style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置textview的背景颜色
- style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //设置指定位置文字的颜色
- textView.setText(style);
二:android string.xml文件中的整型和string型代替:
- String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");
对应的string.xml文件参数:
- <string name="baoxiang">您今天打了%1$d局,还差%2$d局可获得%3$s!</string>
%1$d表达的意思是整个name=”baoxiang”字符串中,第一个整型
三: 其实在Android Developer中有相关说明,可以不用这么麻烦的
使用String 定义如下
<resources> <string name="welcome_messages"><b><font color=\"#ff0000\">红色大写</font></b></string> </resources>
代码中使用如下
String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount); textview.setText(Html.fromHtml(text));
Android Developer 原文:
Styling with HTML markup
You can add styling to your strings with HTML markup. For example:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="welcome">Welcome to <b>Android</b>!</string> </resources>
Supported HTML elements include:
<b>
for bold text.<i>
for italic text.<u>
for underline text.
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String,
method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with
Object...)fromHtml(String)
,
after the formatting takes place. For example:
- Store your styled text resource as an HTML-escaped string:
<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string> </resources>
In this formatted string, a
<b>
element is added. Notice that the opening bracket is HTML-escaped, using the<
notation. - Then format the string as usual, but also call
fromHtml(String)
to
convert the HTML text into styled text:Resources res =
getResources()
; String text = String.format(res.getString(R.string.welcome_messages), username, mailCount); CharSequence styledText = Html.fromHtml(text);
Because the fromHtml(String)
method
will format all HTML entities, be sure to escape any possible HTML characters in the strings you use with the formatted text, using htmlEncode(String)
.
For instance, if you'll be passing a string argument to String.format()
that
may contain characters such as "<" or "&", then they must be escaped before formatting, so that when the formatted string is passed through fromHtml(String)
,
the characters come out the way they were originally written. For example:
String escapedUsername = TextUtil.htmlEncode
(username); Resources res =getResources()
; String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount); CharSequence styledText = Html.fromHtml(text);