开发者

How to display list of resource drawables [duplicate]

开发者 https://www.devze.com 2022-12-25 13:18 出处:网络
This question already has answers here: Retrieving all Drawable resources from Resources object (11 answers)
This question already has answers here: Retrieving all Drawable resources from Resources object (11 answers) 开发者_Go百科 Closed 9 years ago.

I would like to display all resource drawables in a list so that the user can select one. Is there any way to loop through all R.drawable items so I don't have to hard code them into my program?


Using the getFields method on the drawable class, you are able to iterate through the entire list of drawables.

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Reference: http://www.darshancomputing.com/android/1.5-drawables.html


If you look inside your generated R.drawable object you will see that the IDs are contiguous, in my case (and probably yours) beginning at 0x7f020000. As they seem to be sorted by alpha you could probably add dummy images AAAAAAA.png and ZZZZZZ.png and iterate between the two IDs exclusively.

I can't endorse your reasons for attempting this, but I reckon that would work.

0

精彩评论

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