开发者

Android: Accessing list of local resources in Android

开发者 https://www.devze.com 2023-03-21 03:31 出处:网络
How can I access the list of drawables under my project \"res/drawables\"? Im new to Android I\'ve seen an example like this:

How can I access the list of drawables under my project "res/drawables"? Im new to Android I've seen an example like this:

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : dra开发者_C百科wables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But this lists all the default drawables, not what I want:( I want to access those files and have their names to generate an xml, because my layout will be dynamic (number of drawables may differ from time to time).

Thanks for any help.


Replace

android.R.drawable.class.getFields();

with

your.application.package.R.drawable.class.getFields();

to access your drawables.


You are probably wanting Context.getResources...

Accessing Resources


One suggestion would be to follow a naming convention for all your drawables. Say prefix them with your app name 'app'.

for(Field f:drawables){
    if(f.getName().startsWith("app")){
       //Your drawable.
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号