【问题标题】:Iterate through certain images in res/drawable-mdpi遍历 res/drawable-mdpi 中的某些图像
【发布时间】:2015-01-12 20:55:07
【问题描述】:

res/drawable-mdpi 文件夹中,我有 26 个字母图像,分别命名为 big_0.pngbig_25.png

我想将它们全部添加(并忽略文件夹中的任何其他图像)到哈希映射:

private static HashMap<Character, Drawable> sHash = 
    new HashMap<Character, Drawable>();

最初我的计划是:

private static final CharacterIterator ABC = 
    new StringCharacterIterator("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

int i = 0;
for (char c = ABC.first(); 
    c != CharacterIterator.DONE; 
    c = ABC.next(), i++) {

        String fileName = "R.drawable.big_" + i;
        Drawable image = context.getResources().getDrawable(fileName);
        sHash.put(c, image);
}

但后来我意识到 R.drawable.big_0R.drawable.big_25 的类型是 int 而不是 String

所以请告诉我,如何正确遍历 26 张图像?

【问题讨论】:

    标签: android android-resources getresource


    【解决方案1】:

    使用getResources().getIdentifier()"R.drawable.big_" + i 转换为您将与getDrawable() 一起使用的资源ID 值。

    【讨论】:

    • int id = context.getResources().getIdentifier("big_" + i,"drawable", context.getPackageName()); 为我工作,谢谢。 (请注意:当您忘记为可绘制对象调用 setBounds() 时 - 它们不可见)。
    猜你喜欢
    • 1970-01-01
    • 2012-12-13
    • 2013-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多