我在android studio中制作一个应用程序,我想知道是否有一种方法可以制作一个弹出对话框,其中包含类似于GooglePlay商店的GridView以及可绘制文件夹中的所有可绘制对象。
在应用程序中,用户应单击一个按钮,然后将显示对话框,允许他/她选择其个人资料图像。
关于如何遍历所有可绘制对象的任何提示,而不必手动进行?
最佳答案
使用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));
}
关于java - 具有所有可绘制对象的Android Studio对话框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44835287/