/res/values 目录详解
/res/values 下可以放置的文件有:
- strings.xml
- arrays.xml
- dimens.xml
- colors.xml
- styles.xml
2、arrays.xml
详解利用arrays.xml文件存放数组,并读取的过程。
方式1:
<string-array name="proto"> <item>刀兵</item> <item>龙骑</item> <item>黑暗圣堂</item> <item>电兵</item> </string-array> <string-array name="zerg"> <item>小狗</item> <item>飞龙</item> <item>刺蛇</item> </string-array> <string-array name="terran"> <item>机枪兵</item> <item>护士MM</item> <item>幽灵</item> <item>大舰</item> </string-array>
1 int[] resIds = new int[] { R.array.proto, R.array.zerg, R.array.terran }; 2 String[] armType = new String[] { "神族兵种:", "虫族兵种:", "人族兵种:" }; 3 getArms(resIds, armType); 4 5 private void getArms(int[] ids, String[] typeNames) { 6 int len1 = ids.length; 7 String[][] arms = new String[len1][]; 8 9 for (int i = 0; i < len1; i++) { 10 String[] armsType = this.getResources().getStringArray(ids[i]); 11 int len2 = armsType.length; 12 arms[i] = new String[len2]; 13 System.out.println(typeNames[i]); 14 for (int j = 0; j < len2; j++) { 15 arms[i][j] = armsType[j]; 16 System.out.print(arms[i][j] + " "); 17 } 18 System.out.println(); 19 } 20 }
输出结果:
方式2:
<string-array name="planet"> <item>太阳</item> <item>水星,金星,地球,火星,木星,土星,天王星,海王星</item> <item>哈雷,恩克</item> </string-array>
1 String[] planetType = new String[] { "恒星:", "行星:", "彗星:" }; 2 getPlanets(planetType); 3 4 private void getPlanets(String[] typeNames) { 5 String[] ids = this.getResources().getStringArray(R.array.planet); 6 int len1 = ids.length; 7 String[][] planets = new String[len1][]; 8 for (int i = 0; i < len1; i++) { 9 String[] planetArr = ids[i].split(","); 10 int len2 = planetArr.length; 11 planets[i] = new String[len2]; 12 System.out.println(typeNames[i]); 13 for (int j = 0; j < len2; j++) { 14 planets[i][j] = planetArr[j]; 15 System.out.print(planetArr[j]+" "); 16 } 17 System.out.println(); 18 } 19 }
输出结果:
方式3:
<string-array name="china"> <item>@array/guangdong</item> <item>@array/beijing</item> <item>@array/yunnan</item> </string-array> <string-array name="guangdong"> <item>广州</item> <item>深圳</item> </string-array> <string-array name="beijing"> <item>北京</item> </string-array> <string-array name="yunnan"> <item>昆明</item> <item>大理</item> <item>丽江</item> </string-array>
1 String[] province = new String[] { "广东:", "北京:", "云南:" }; 2 getCities(province); 3 4 private void getCities(String[] names) { 5 TypedArray typedArray = getResources().obtainTypedArray(R.array.china); 6 int len1 = typedArray.length(); 7 String[][] cities = new String[len1][]; 8 for (int i = 0; i < len1; i++) { 9 int resArrID = typedArray.getResourceId(i, 0); 10 String[] cityArr = this.getResources().getStringArray(resArrID); 11 int len2 = cityArr.length; 12 cities[i] = new String[len2]; 13 System.out.println(names[i]); 14 for (int j = 0; j < len2; j++) { 15 cities[i][j] = cityArr[j]; 16 System.out.print(cityArr[j]+" "); 17 } 18 System.out.println(); 19 } 20 }
输出结果:
对于不同数据类型的读取:
<string-array name="FourPoets"> <item>唐伯虎</item> <item>祝枝山</item> <item>文征明</item> <item>徐祯卿</item> </string-array>
<integer-array name="capability"> <item>99</item> <item>96</item> <item>90</item> <item>85</item> </integer-array> <array name="sex"> <item>男</item> <item>女</item> </array> CharSequence[] name = getResources().getTextArray(R.array.FourPoets); int[] capability = getResources().getIntArray(R.array.price); String[] sex = getResources().getStringArray(R.array.sex);
默认使用<array>标签,item里面内容需以String数组形式读出。