关于Android——integer-array存放R.draw

2019-11-12  本文已影响0人  仙狗许小格

在xml中储存图片id的引用:

<integer-array name="chose_color">
<item>@drawable/round_corner_color</item>
 <item>@drawable/round_corner_color1</item>
 <item>@drawable/round_corner_color2</item>
</integer-array>

用getIntArray发现数组中都为0

错误代码:

  int[] task = getResources().getIntArray(R.array.background);
  for(int i:task){
    //结果全为0
   System.out.println(task[i]);
  }

正确代码:

TypedArray pictures = getResource().obtainTypedArray(R.array.pictures);
Drawable drawable = pictures.getDrawable(0);


总结:

xml存储id引用使用array 或integer-array 都可以,关键是要用TypeArray来解析。

上一篇 下一篇

猜你喜欢

热点阅读