[英]Reference the INTEGER value of a string resource in XML
Is it possible to reference not the representated string, but the integer key of a resource in xml? 是否可以引用表示的字符串,而不是xml中资源的整数键?
<string name="first_string">First! :-D</string>
<string name="second_string">Second :(</string>
<integer-array name="something">
<item>@string/first_string</item>
<item>@string/second_string</item>
</integer-array>
This for example does not work, because @string/first_string
is resolved to First! :-D
例如,这不起作用,因为
@string/first_string
解析为First! :-D
First! :-D
, instead of it's resource key (for example 0x7f0c0010
). First! :-D
,而不是它的资源密钥(例如0x7f0c0010
)。 Is there any way at all to access that? 有什么办法可以访问吗?
In java, you have integer values in generated R.class of your project. 在Java中,项目的生成的R.class中具有整数值。
For instance: 例如:
int value = R.string.first_string;
In XML, it's not possible to get it. 在XML中,无法获得它。
One thing that has a similar effect as putting the keys in an xml array, is by accessing it in java with obtainTypedArray
instead of getIntArray
or getStringArray
. 与将键放入xml数组具有类似效果的一件事是,使用Java中的
obtainTypedArray
而不是getIntArray
或getStringArray
。
typedArray = getResources().obtainTypedArray(R.array.something);
int id = typedArray.getResourceId(0, -1); // 1st param: position, 2nd: default value
if (id == R.string.first_string); // true
if (id == R.string.second_string); // false
Hope that helps someone! 希望能对某人有所帮助! For me (op), this seems not to help at all, since I can't access the stored preferences in this way, which is why I needed it in XML directly if at all possible.
对于我(op)来说,这似乎根本没有帮助,因为我无法以这种方式访问存储的首选项,这就是为什么我尽可能直接在XML中使用它的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.