在处理RES /值的Andr​​oid动态文本字符串 [英] Handling Android dynamic text string in res/values

查看:177
本文介绍了在处理RES /值的Andr​​oid动态文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个字符串:

您的播放器有一个门将技能和体面人的技能

现在的部分不以粗体字符串的总是相同的,所谓在运行时是部分以粗体

所以,我怎么会做这样的事情:

您的播放器有一个 {VAR1} 门将技能和 {} VAR2 人的技能

然后填补这些增值经销商在运行时用正确的价值观?

我不想来连接字符串一样:

 你的球员有一个+ VAR1 +门将的技能和一个+ VAR2 +人艺
 

解决方案

您需要看到的Andr​​oid字符串资源指南。有一种方法是提供一种可以通过使用变量稍后格式化静态字符串

<一个href="http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling">http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

您将定义字符串,如

 &LT;资源&GT;
  &LT;字符串名称=welcome_messages&GT;您好,%1 $ S!你必须和放大器; LT; B&GT;%2 $ D新邮件和放大器; LT; / B&GT;&LT; /串&GT;
&LT; /资源&GT;
 

和后来在code可以更换

 资源资源= getResources();
字符串文本=的String.Format(res.getString(R.string.welcome_messages),用户名,mailCount);
CharSequence中的StyledText = Html.fromHtml(文本);
 

Let's say I have this string:

Your player has a good keeper skill and a decent people skill

Now the part not in bold of the string is always the same, what is known at runtime is the part in bold.

So how could I do something like:

Your player has a {var1} keeper skill and a {var2} people skill

and then fill those vars at runtime with right values?

I do not want to concatenate strings like:

"You player has a"  + var1  + "keeper skill and a"  + var2 + "people skill"

解决方案

You need to see Android string resource guide. There is a way to provide static string which can be later formatted with variables.

http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

You will define string like

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

And later in code you can replace

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

这篇关于在处理RES /值的Andr​​oid动态文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆