简体   繁体   English

具有所有可绘制对象的Android Studio对话框

[英]Android Studio Dialog With All Drawables

I am making an app in android studio and i was wondering if there is a way to make a pop-up dialog containing a GooglePlay Store-like GridView with all the drawables in the drawable folder. 我在android studio中制作一个应用程序,我想知道是否有一种方法可以制作一个弹出对话框,其中包含类似于GooglePlay商店的GridView以及可绘制文件夹中的所有可绘制对象。 In the app, the user should click on a button and then the dialog will show, allowing him/her to choose their profile image. 在应用程序中,用户应单击一个按钮,然后将显示对话框,允许他/她选择其个人资料图像。 Any tips on how to iterate through all the drawables and not having to do this manually? 关于如何遍历所有可绘制对象且无需手动执行此操作的任何提示?

Use the R.drawables class and use reflection to get its fields. 使用R.drawables类并使用反射来获取其字段。 something like this . 这样 Then use getResources().getIdentifier() to get the resource id. 然后使用getResources()。getIdentifier()获取资源ID。 I may be able to post some code if you are interested 如果您有兴趣,我可能可以发布一些代码


Some code to get you started(copied shamelessly from here and modified to make it to a static class) 一些使您入门的代码(从这里无耻地复制,并进行了修改以使其成为静态类)

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;
    }
}


Then you can just use the following code in any activity or fragment(or anywhere a context is available. 然后,您可以在任何活动或片段(或任何可用上下文的地方)中使用以下代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM