<integer-array name="ar_ex_1">
    <item>@drawable/ar_001_main</item>
    <item>@drawable/ar_001_1</item>
    <item>@drawable/ar_001_2</item>
    <item>@drawable/ar_001_3</item>
    <item>@drawable/ar_001_4</item>
    <item>@drawable/ar_001_5</item>
</integer-array>


我有这个整数数组,并使用以下命令将其加载到Java中:

int[] arr = getResources().getIntArray(R.array.ar_ex_1);
for(int i: arr) {
   Log.e("anindya", ""+i);
}


输出为:

06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0
06-04 20:31:57.045 9360-9360/edu.usc.projecttalent.cognitive E/anindya: 0


我期望整数((drawableR.java的值,但是我只能得到零。这不是加载可绘制数组的方法吗?

最佳答案

如果这是您在array.xml中的可绘制数组,请尝试这种代码。

 <integer-array name="ar_ex_1">
        <item>@drawable/ar_001_main</item>
        <item>@drawable/ar_001_1</item>
        <item>@drawable/ar_001_2</item>
        <item>@drawable/ar_001_3</item>
        <item>@drawable/ar_001_4</item>
        <item>@drawable/ar_001_5</item>
    </integer-array>


然后在您的活动中,像这样访问它们:

TypedArray imgs = getResources().obtainTypedArray(R.array.ar_ex_1);

// get resource ID by index
imgs.getResourceId(i, -1)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));

// recycle the array
imgs.recycle();

关于java - 可绘制ID可乐零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44361313/

10-10 12:41