从另一个字符串中的strings.xml引用一个字符串? [英] Reference one string from another string in strings.xml?

查看:753
本文介绍了从另一个字符串中的strings.xml引用一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想引用一个字符串从另一字符串在我的strings.xml文件,如下图所示(特别注意的MESSAGE_TEXT字符串内容的末尾):

I would like to reference a string from another string in my strings.xml file, like below (specifically note the end of the "message_text" string content):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="button_text">Add item</string>
    <string name="message_text">You don't have any items yet! Add one by pressing the \'@string/button_text\' button.</string>
</resources>

我已经试过了上面的语法但随后的文本打印出来的@字符串/ BUTTON_TEXT以明文形式。不是我想要的。我想打印消息文本您没有任何物品呢!添加一个接pressing添加项目按钮。

I've tried the above syntax but then the text prints out the "@string/button_text" as clear text. Not what I want. I would like the message text to print "You don't have any items yet! Add one by pressing the 'Add item' button."

有没有已知的方法可以达到我想要的吗?

Is there any known way to achieve what I want?

理由:
我的应用程序的项目清单,但是当该列表是空的我呈现@android:ID /空的TextView代替。在于TextView中的文字是通知用户如何添加新的项目。我想提出我的布局防呆变化(是的,我是傻瓜的问题: - )

RATIONALE:
My application has a list of items, but when that list is empty I show a "@android:id/empty" TextView instead. The text in that TextView is to inform the user how to add a new item. I would like to make my layout fool-proof to changes (yes, I'm the fool in question :-)

推荐答案

我认为你不能。但是,你可以在格式化的字符串,只要你喜欢:

I think you can't. But you can "format" a string as you like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="button_text">Add item</string>
    <string name="message_text">You don't have any items yet! Add one by pressing the %1$s button.</string>
</resources>

在code:

Resources res = getResources();
String text = String.format(res.getString(R.string.message_text),
                            res.getString(R.string.button_text));

这篇关于从另一个字符串中的strings.xml引用一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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