我正在尝试了解一款对投资组合进行计算的 Android 应用。投资组合存储在 res/values/portfolio.xml
中:
当在应用程序中按下一个按钮时,投资组合数据被检索如下:
String portfolioName = ((TextView) findViewById(R.id.portfolioName)).getText().toString();
Resources res = getResources();
String[] data = res.getStringArray(res.getIdentifier(portfolioName, "array", this.getPackageName()));
我在 String Array 上找到了 Android 文档解释 portfolio.xml
语法的资源类型文件,它解释了为什么 name
属性应用作 getIdentifier()
的第一个参数:
“The filename is arbitrary. The
<string-array>
element'sname
will be used as the resource ID.”
但我还没有找到任何文档来说明您如何知道应该为 defType
放置什么? getIdentifier
的参数(除此之外它是一个字符串)。在提供的示例中,"array"
有效,但它来自哪里?一般而言,“defType”的可能值是什么?
最佳答案
getIdentifier
返回给定资源名称的资源的 id
。 typeDef 指的是 Resource
的类型(阅读更多 here )。请记住,res
的内容是在编译时解析的,R.java 类是根据解析结果生成的。最后,您要查找的是在该类中声明的字段。我不知道内部实现,但如果你提供 array
作为 res 类型,android 将只查找 R.array
,而不是整个 R
关于android - Resources.getIdentifier(),deftype 参数的可能值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756896/