gpt4 book ai didi

android:引用自定义xml中的资源

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:21 26 4
gpt4 key购买 nike

我有一个多语言 android 应用程序,我将不同的翻译放在相应目录的 strings.xml 中。

现在我还有一个自定义的 xml 文件,我想在其中引用这样的文本:

<?xml version="1.0" encoding="UTF-8"?>
<rooms>
<room title="@+string/localizedtext" />
</rooms>

现在,当我读取代码中的 title 属性时,我显然得到了未解析的字符串“@+string/localizedtext”。是否有可能以某种方式自动解析此链接到本地​​化文本?

谢谢!

最佳答案

将近一年后:

    public static String getStringResource(Context context, String thingie) {
try {
String[] split = thingie.split("/");
String pack = split[0].replace("@", "");
String name = split[1];
int id = context.getResources().getIdentifier(name, pack, context.getPackageName());
return context.getResources().getString(id);
} catch (Exception e) {
return thingie;
}
}

这样就可以了。

关于android:引用自定义xml中的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4267766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com