如何将drawable转换为int数组

3

我有一个int数组,像这样:

private int icons[] = new int[] {R.drawable.itr300, R.drawable.itr3500};

但是我想把它放在array.xml中。

如果我这样做:

<resources>
<array name="icons">
    <item>@drawable/itr300</item>
    <item>@drawable/itr3500</item>
</array>
</resources>

private TypedArray images = getResources().obtainTypedArray(R.array.icons);
private int icons[] = images...

现在我想将Drawable转换为int数组,但我不知道该怎么做。能帮忙吗? 评论: 我认为你的答案很有用,但我还没有找到答案。你能再次帮助我找到语法错误吗?看起来很好,但它不起作用。
   private TypedArray images = getResources().obtainTypedArray(R.array.icons);
   private int[] icons = new int[images.length()];  
    for (int i = 0; i < icons.length(); i++) {
        icons[i] = images.getResourceId(i, 0);
    }

如果您已经得到答案,请将正确的答案标记为接受的。 - MorningGlory
3个回答

3

0
public int getInt (int index, int defValue)   : Retrieve the integer value for the attribute at index.

参数

index 属性的索引。

defValue 如果未定义属性,则返回的值。

public int getInteger (int index, int defValue)

参数

index 要检索的属性的索引。

defValue 如果未定义属性或不是资源,则返回的值

for (int i = 0; i < icons.length; i++) {

        icons[i]  = images.getInt (i, int defValue) 
    }

谢谢您的回答。但是我没有得到一个有效的答案。因此,我在我的问题下添加了一条评论。 - FelixA

0

像这样做

        public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = null;

        if(convertView == null)
        {
            LayoutInflater i = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
            row = i.Inflate(Resource.Layout.custom_menu, parent, false);
        }
        else
        {
            row = convertView;
        }

        TextView title = row.FindViewById<TextView>(Resource.Id.menu_title);
        ImageView icon = row.FindViewById<ImageView>(Resource.Id.menu_icon);


        title.Text = menuTitle[position];
        //icon.SetImageResource(menuIcon[position]);
        icon.SetImageResource(Icon.GetResourceId(position,-1));

        return row; 
    }

Icon.getResourceId中的图标实际上来自于TypedArray Icon = context.Resources.ObtainTypedArray(Resource.Array.menu_icon); - user3693644
请添加解释以提高答案的价值。 - Aminah Nuraini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接