android xml中应用占位符

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:

<string
 
name
=
"good_example"
>
"This'll work"
</string>


<string
 
name
=
"good_example_2"
>
This\'ll also work
</string>


<string
 
name
=
"bad_example"
>
This doesn't work
</string>


<string
 
name
=
"bad_example_2"
>
XML encodings don&apos;t work
</string>

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:

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

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"
?>


<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 with fromHtml(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
);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值