阅读更多

Formatting and Styling

Here are a few important things you should know about how to properly format and style your string resources.

Escaping apostrophes and quotes

If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:

 name="good_example">"This'll work" name="good_example_2">This\'ll also work name="bad_example">This doesn't work name="bad_example_2">XML encodings don't work

Formatting strings

If you need to format your strings using String.format(String, Object...) , then you can do so by putting your format arguments in the string resource. For example, with the following resource:

 name="welcome_messages">Hello, %1$s! You have %2$d new messages.

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguements from your application like this:

Resources res = getResources();String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Styling with HTML markup

You can add styling to your strings with HTML markup. For example:

<?xml version="1.0" encoding="utf-8"?>     name="welcome">Welcome to Android!

Supported HTML elements include:

  • for bold text.
  • for italic text.
  • 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 with fromHtml(String) , after the formatting takes place. For example:

  1. Store your styled text resource as an HTML-escaped string:
       name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.

    In this formatted string, a element is added. Notice that the opening bracket is HTML-escaped, using the < 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);

更多相关文章

  1. Android的多媒体框架Opencore代码阅读
  2. 【博客园客户端】博客园Android客户端更新:离线下载、本地收藏、R
  3. Android应用层源码阅读笔记--Application
  4. 在Android上模拟MetroUI
  5. android按钮按下的效果
  6. Android: Actions for BroadcastReceiver
  7. android sdk
  8. Android版本和API Level对应关系
  9. 学习Android的几大主攻方向

随机推荐

  1. Java事件模型与Android事件模型的比较
  2. Android音乐播放器系列讲解之一
  3. Android(安卓)4.4 KitKat升级率已经接近1
  4. [Android(安卓)新特性] 改进明显 Android
  5. android之使用mvn构建创造项目步骤
  6. 从零开始--系统深入学习android(实践-让我
  7. ImageView的属性android:scaleType
  8. Android进程 与 消息模型
  9. Android的ps命令介绍和技巧
  10. 源码分析Android的消息机制