现在的位置: 首页 > 综合 > 正文

Android TextView 展示多种文字样式

2013年11月17日 ⁄ 综合 ⁄ 共 2916字 ⁄ 字号 评论关闭

一:TextView组件改变部分文字的颜色:

 

Java代码  收藏代码
  1. TextView textView = (TextView)findViewById(R.id.textview);  
  2.   
  3. //方法一:  
  4. textView.setText(Html.fromHtml("<font color=\"#ff0000\">红色</font>其它颜色"));  
  5.   
  6. //方法二:  
  7.  String text = "获得银宝箱!";  
  8.  SpannableStringBuilder style=new SpannableStringBuilder(text);     
  9.   style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置textview的背景颜色  
  10.   style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色  
  11.   textView.setText(style);   

 

 

 

 

二:android string.xml文件中的整型和string型代替:

 

Java代码  收藏代码
  1. String text = String.format(getResources().getString(R.string.baoxiang), 2,18,"银宝箱");  

 

 对应的string.xml文件参数:

 

Xml代码  收藏代码
  1. <string name="baoxiang">您今天打了%1$d局,还差%2$d局可获得%3$s!</string>  

 %1$d表达的意思是整个name=”baoxiang”字符串中,第一个整型

 

三:  其实在Android Developer中有相关说明,可以不用这么麻烦的

        使用String 定义如下

<resources>
  <string name="welcome_messages">&lt;b>&lt;font color=\"#ff0000\">红色大写&lt;/font>&lt;/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,
Object...)
 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 withfromHtml(String),
after the formatting takes place. For example:

  1. Store your styled text resource as an HTML-escaped string:

    <resources>
      <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
    </resources>

    In this formatted string, a <b> element is added. Notice that the opening bracket is HTML-escaped, using the&lt; notation.

  2. 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);

抱歉!评论已关闭.