Android动态获取资源ID

该代码段展示了如何在Android中动态获取不同类型的资源ID,如anim、animator、attr等,通过ResourceUtils工具类提供了一系列静态方法,简化了资源ID的获取过程。这些方法接受Context、资源类型和应用包名为参数,返回相应的资源ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

API:getIdentifier("资源ID名称","资源类型","应用包名");

getIdentifier("headImg","drawable","com.demo.xxxx");

/**
 * 动态获取资源ID
 */
public class ResourceUtils {

    public static int getAnimId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "anim", context.getPackageName());
    }
    public static int getAnimatorId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "animator", context.getPackageName());
    }
    public static int getAttrId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "attr", context.getPackageName());
    }

    public static int getBoolId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "bool", context.getPackageName());
    }

    public static int getColorId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "color", context.getPackageName());
    }

    public static int getDimenId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "dimen", context.getPackageName());
    }

    public static int getDrawableId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "drawable", context.getPackageName());
    }

    public static int getId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "id", context.getPackageName());
    }

    public static int getIntegerId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "integer", context.getPackageName());
    }

    public static int getInterpolatorId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "interpolator", context.getPackageName());
    }

    public static int getLayoutId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "layout", context.getPackageName());
    }

    public static int getPluralsId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "plurals", context.getPackageName());
    }

    public static int getStringId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "string", context.getPackageName());
    }

    public static int getStyleId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "style", context.getPackageName());
    }

    public static int getStyleableId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "styleable", context.getPackageName());
    }

    public static int getXmlId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "xml", context.getPackageName());
    }

    public static int getMipmapId(Context context, String defType) {
        return context.getResources().getIdentifier(defType, "mipmap", context.getPackageName());
    }

    /**
     * 通过反射来读取int[]类型资源Id
     * @param context
     * @param name
     * @return
     */
    public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) {
        try {
            Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable" ).getFields();
            for (Field f : fields2 ){
                if (f.getName().equals(name)){
                    int[] ret = (int[])f.get(null);
                    return ret;
                }
            }
        }
        catch (Throwable t){
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值