Skip to content

Instantly share code, notes, and snippets.

@brucetoo
Created December 7, 2015 10:34
Show Gist options
  • Save brucetoo/48be42fa1387717547e2 to your computer and use it in GitHub Desktop.
Save brucetoo/48be42fa1387717547e2 to your computer and use it in GitHub Desktop.
通过资源名获取资源ID
package com.netease.cc.mgsdk.util;
import java.lang.reflect.Field;
import android.app.Activity;
import android.content.Context;
import android.view.View;
public class IdentifierUtil {
/**
* 获取xml的layout文件的id
*
* @param context
* @param layoutName
* @return
*/
public static int getLayoutId(Context context, String layoutName) {
int identifier = context.getResources().getIdentifier(layoutName, "layout", context.getPackageName());
return identifier;
}
/**
* 获取string类型的view id获取int类型的view id
*
* @param context
* @param viewId
* @return
*/
public static int getViewId(Context context, String viewId) {
return context.getResources().getIdentifier(viewId, "id", context.getPackageName());
}
public static View getView(Activity activity, String viewId) {
int vid = getViewId(activity, viewId);
if (vid > 0) {
return activity.findViewById(vid);
} else {
return null;
}
}
public static int getStringId(Context context, String stringId) {
return context.getResources().getIdentifier(stringId, "string", context.getPackageName());
}
public static int getArrayId(Context context, String stringArrayId) {
return context.getResources().getIdentifier(stringArrayId, "array", context.getPackageName());
}
public static String getString(Context context, String stringId) {
int sid = getStringId(context, stringId);
if (sid > 0) {
return context.getResources().getString(sid);
} else {
return "";
}
}
public static int getAnimId(Context context, String AnimatorName) {
return context.getResources().getIdentifier(AnimatorName, "anim", context.getPackageName());
}
public static int getAnimatorId(Context context, String AnimatorName) {
return context.getResources().getIdentifier(AnimatorName, "animator", context.getPackageName());
}
public static int getDrawableId(Context context, String drawableName) {
return context.getResources().getIdentifier(drawableName, "drawable", context.getPackageName());
}
public static int getStyleId(Context context, String styleName) {
return context.getResources().getIdentifier(styleName, "style", context.getPackageName());
}
public static float getDimension(Context context, String dimenId) {
return context.getResources().getDimension(getDimenId(context, dimenId));
}
public static int getColor(Context context, String colorId) {
int id = context.getResources().getIdentifier(colorId, "color", context.getPackageName());
return context.getResources().getColor(id);
}
public static int getDimenId(Context context, String dimenId) {
return context.getResources().getIdentifier(dimenId, "dimen", context.getPackageName());
}
/**
*
* 对于 context.getResources().getIdentifier 无法获取的数据 , 或者数组
*
* 资源反射值
*
* @paramcontext
*
* @param name
*
* @param type
*
* @return
*/
private static Object getResourceId(Context context, String name, String type) {
String className = context.getPackageName() + ".R";
try {
Class<?> cls = Class.forName(className);
for (Class<?> childClass : cls.getClasses()) {
String simple = childClass.getSimpleName();
if (simple.equals(type)) {
for (Field field : childClass.getFields()) {
String fieldName = field.getName();
if (fieldName.equals(name)) {
System.out.println(fieldName);
return field.get(null);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* context.getResources().getIdentifier 无法获取到 styleable 的数据
*
* @paramcontext
*
* @param name
*
* @return
*/
public static int getStyleable(Context context, String name) {
return ((Integer) getResourceId(context, name, "styleable")).intValue();
}
/**
*
* 获取 styleable 的 ID 号数组
*
* @paramcontext
*
* @param name
*
* @return
*/
public static int[] getStyleableArray(Context context, String name) {
return (int[]) getResourceId(context, name, "styleable");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment