本文介绍了动态字符串使用String.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在 string.xml 中的字符串值的占位符,可以在运行时赋值?

例如:

解决方案

格式和样式

是的,看到String资源:格式化和样式

注意的getString 有一个使用字符串作为格式字符串的过载:

 字符串文本= res.getString(R.string.welcome_messages,用户名,mailCount);
 

复数

如果您需要处理复数,使用:

 <复数名=welcome_messages>
    <项目数量=一>您好,%1 $ S!你有一个新的消息< /项目>
    <项目数量=其他>您好,%1 $ S!你有2%$ D新消息< /项目>
< /复数>
 

第一mailCount param用于决定使用(单个或多个)的格式,其他PARAMS是你的替换:

 资源资源= getResources();
字符串文本= res.getQuantityString(R.plurals.welcome_messages,
     mailCount,用户名,mailCount);
 

请参阅字符串资源:复数的更多细节。

Is it possible to have placeholders in string values in string.xml that can be assigned values at run time?

Example:

解决方案

Formatting and Styling

Yes, see the following from String Resources: Formatting and Styling

Note that getString has an overload that uses the string as a format string:

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

Plurals

If you need to handle plurals, use this:

<plurals name="welcome_messages">
    <item quantity="one">Hello, %1$s! You have a new message.</item>
    <item quantity="other">Hello, %1$s! You have %2$d new messages.</item>
</plurals>

The first mailCount param is used to decide which format to use (single or plural), the other params are your substitutions:

Resources res = getResources();
String text = res.getQuantityString(R.plurals.welcome_messages,
     mailCount, username, mailCount);

See String Resources: Plurals for more details.

这篇关于动态字符串使用String.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:06
查看更多