如何在Android中按名称从类型化数组资源中获取值?
假设我有一个像这样的资源xml文件:
1 2 2 2 4 3 3 6 3
如何从名称中获取项目的整数值? TypedValue
的API似乎不包含任何方法。 如果使用TypedArray
不可能这是怎么回事?
如果我可以按顺序从项目中获取值,那也可以。
我不记得这是可能的(但我可能是错的)。 根据项目的结构(在difficulties
数组中)你可以做其他事情,你可以使用整数数组的数组。 知道项目数组在第一个位置具有level
, fixed_blocks
作为第二个位置等,您可以轻松获取值。 你可以在这里找到一个例子Android Resource – Array of Array
编辑:这是您正在寻找的方法吗?
private int[] getLevelConstants(int level) { int[] result = null; TypedArray ta = getResources().obtainTypedArray(R.array.difficulties); // keep in mind the differences between the level(starts at 1) and the index of the difficulties array which starts at 0 int id = ta.getResourceId(level, 0); if (id > 0) { result = getResources().getIntArray(id); } else { // something bad } return result; }
并且数组将是:
- @array/level1
- @array/level2
- 1
- 2
- 2
- 2
- 4
- 3