如何连接在机器人XML多个字符串? [英] How to concatenate multiple strings in android XML?

查看:130
本文介绍了如何连接在机器人XML多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题
我希望能够做这样的事情在Android的XML code:

Problem
I would like to be able to do something like that in Android XML code:

<string name="title">@string/app_name test</string>
<string name="title2">@string/app_name @string/version_name</string>

至于code编译器的第一行显示这个错误

Regarding first line of code compiler shows this error

没有资源的发现,(与'@字符串/ APP_NAME试验价值的'标题')

No resource found that matches the given name (at 'title' with value '@string/app_name test')

和关于code编译器的第二行显示此错误

and regarding second line of code compiler shows this error

没有资源的发现,匹配给定的名称(在标题2,值为@字符串/ APP_NAME @字符串/ VERSION_NAME')

No resource found that matches the given name (at 'title2' with value '@string/app_name @string/version_name')

问题
是否有人知道如何来连接多个字符串在Android的XML?

Question
Does anybody know how to concatenate multiple strings in Android XML?

依据
这是不好的做法,在code多的地方(在这种情况下,XML)重复的变量值。

Rationale
It is bad practice to duplicate variable values in many places of code (in this case XML).

推荐答案

我试图做类似的动作自己,但有人告诉我,这是不是可行的Andr​​oid系统。

I've tried to do a similar maneuver myself but I was told this is not doable in Android.

有趣的是,你会得到一个错误消息,表明它确实的的可能,但由于资源的错误匹配这是不可能的现在。 (你确定你所定义的APP_NAME和之前的冠军VERSION_NAME字符串和标题2的字符串?)

The interesting thing is that you get an error message that indicates that it indeed is possible but due to errors in resource matching it's not possible right now. (Are you sure you have defined the "app_name" and "version_name" strings before the "title" and "title2" strings?)

然而,你可以这样做:

<string name="title">%1$s test</string>
<string name="title2">%1$s %2$s</string>

<string name="app_name">AppName</string>
<string name="version_name">1.2</string>

然后再从Java code做这样的事情:

And then from Java code do something like:

Resources res = getResources();
String appName = res.getString(R.string.app_name);
String versionName = res.getString(R.string.version_name);

String title = res.getString(R.string.title, appName);
String title2 = res.getString(R.string.title2, appName, versionName);

更多关于<一个href="http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling">formatting在Android的字符串。

希望这有助于。

这篇关于如何连接在机器人XML多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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