所有Drawables的Android Studio对话框

时间:2017-06-29 21:30:20

标签: java android dialog

我正在android studio制作应用程序,我想知道是否有办法制作一个包含GooglePlay Store-like GridView的弹出式对话框,其中包含drawable文件夹中的所有drawable。 在应用程序中,用户应单击按钮,然后对话框将显示,允许他/她选择他们的个人资料图像。 有关如何遍历所有drawable并且不必手动执行此操作的任何提示?

1 个答案:

答案 0 :(得分:2)

使用R.drawables类并使用反射来获取其字段。 像this这样的东西。然后使用getResources().getIdentifier()获取资源ID。如果您有兴趣,我可能会发布一些代码


一些代码可以帮助您入门(从here无耻地复制并修改为使其成为静态类)

package com.example.android.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.lang.reflect.Field;

public class ResourceReflectionUtils {
    private static final String TAG = ResourceReflectionUtils.class.getName();
    private final static String RESOURCE_LOCATION_DRAWABLES = ".R.drawable";
    private final static String RESOURCE_LOCATION_LAYOUT = ".R.layout";
    private final static String RESOURCE_LOCATION_STRING = ".R.string";

    public static Class<?> getResourceClass(Context context, final String suffix) throws PackageManager.NameNotFoundException {
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            try {
                final Class<?> rClassBase = Class.forName(info.packageName + ".R");
                final Class<?>[] subClassTable = rClassBase.getDeclaredClasses();

                for (Class<?> subClass : subClassTable) {
                    if (subClass.getCanonicalName().endsWith(suffix)) {
                        return subClass;
                    }
                }
            } catch (ClassNotFoundException e) {
                Log.e(TAG, "getResourceClass() ClassNotFoundException: " + e.getMessage(), e);
            }

            Log.e(TAG, "getResourceClass() Unable to find Sublass: " + suffix);

            return null;
    }

    public static void logFields(Context context, String subclassname) {
        try {
            final Field[] fields = getResourceClass(context, subclassname).getFields();
            for (Field field : fields) {
                Log.d(TAG, "logFields() Field: '" + field.getName() + "'");
            }
        } catch (NullPointerException | PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void logSubClasses(String baseClass) {
        Log.d(TAG, "logSubClasses() Getting subclasses for '" + baseClass + "' ============= ");

        try {
            final Class<?> rClass = Class.forName(baseClass);
            final Class<?>[] subClassTable = rClass.getDeclaredClasses();

            for (final Class<?> subclass : subClassTable) {
                Log.d(TAG, "logSubClasses() Class: " + subclass.getCanonicalName());
            }

        } catch (Exception e) {
            Log.e(TAG, "logSubClasses() Error: " + e.getMessage(), e);
        }
    }

    public static int[] getDrawableIds(Context context) {
       return getIds(context, RESOURCE_LOCATION_DRAWABLES);
    }

    public static int[] getIds(Context context, String subclass) {
        int[] ids = null;
        try {
            final Field[] fields = getResourceClass(context, subclass).getFields();
            ids = new int[fields.length];
            int ii = 0;
            for (Field field : fields) {
                field.setAccessible(true);
                ids[ii] = field.getInt(null);
                Log.d(TAG, "logFields() Field: '" + field.getName() + "'");
                ii++;
            }
        } catch (NullPointerException | PackageManager.NameNotFoundException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        return ids;
    }

    protected static int reflectDrawable(Context context, String fieldName, int defaultValue, boolean reportFailure) {
        return reflectResource(context, RESOURCE_LOCATION_DRAWABLES, fieldName, defaultValue, reportFailure);
    }

    private static int reflectResource(Context context, String resourceLocation, String fieldName, int defaultValue, boolean reportFailure) {
        int error = 0;
        try {
            final Field field = getResourceClass(context, resourceLocation).getField(fieldName);
            return field.getInt(null);
        } catch (NoSuchFieldException e) {
            error = 1;
        } catch (IllegalAccessException e) {
            error = 2;
        } catch (NullPointerException e) {
            error = 3;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (reportFailure) {
            Log.w(TAG, "reflectResource() Resource '" + fieldName + "' not available! (" + error +")");
        }

        return defaultValue;
    }
}


然后,您可以在任何活动或片段中使用以下代码(或在上下文可用的任何位置使用。

   int ids[] = ResourceReflectionUtils.getDrawableIds(this);
   for(int id: ids) {
        Log.i(TAG, context.getResources().getResourceName(id));
   }