Android 开发技巧 - 通过getIdentifier()方法实现在Module中使用Application资源

转载请注明出处,感谢您的支持。同时,欢迎加入移动开发学习交流qq群 : 450302004,互相学习。
文章来源:【chenyk的简书】http://www.jianshu.com/p/282b0d1c142c

前言

在项目的开发过程中,对于资源的使用,无非有以下几种情况:

1、Application使用自身资源
2、Application使用Module资源
3、Module使用自身或另外的Module资源
4、Module使用Application资源

前3项在这里就不过多介绍,这里主要说下第4项,有些时候我们需要在抽取的module中使用到Application的资源,而且我们并不想在Module中重新定义。如果直接调用的话,是调用不到的。所以我们需要通过getIdentifier()方法来实现,具体如下:

  /**
     * 获取context中对应类型的资源id
     *
     * @param context
     * @param type 资源类型,"layout","string","drawable","color"等
     * @param resName
     * @return
     */
    private static int getResId(Context context, String type, String resName) {
        return context.getResources().getIdentifier(resName, type, context.getPackageName());
    }

举个栗子,在下图中,包含2个Application(app/doctor)5个Module(calendarview/commlib/date_picker/MPChartLib/NativeH5Lib),如果要在commlib使用app的资源,就使用以上的方法去获取。

项目结构.png

调用方式

调用方法非常简单,只需传入context和res名称即可,如下所示:

ResourceUtil.getColorResId(this, "colorPrimary"); //colorPrimary是color.xml文件中定义的资源

完整的工具类

import android.content.Context;

/**
 * Created by chenyk on 2017/6/2.
 * 资源工具类
 * 功能:module中根据context动态获取application中对应资源文件
 */

public class ResourceUtil {
    /**
     * 获取布局资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getLayoutResId(Context context, String resName) {
        return getResId(context, "layout", resName);
    }

    /**
     * 获取字符串资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getStringResId(Context context, String resName) {
        return getResId(context, "string", resName);
    }

    /**
     * 获取Drawable资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getDrawableResId(Context context, String resName) {
        return getResId(context, "drawable", resName);
    }

    /**
     * 获取颜色资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getColorResId(Context context, String resName) {
        return getResId(context, "color", resName);
    }

    /**
     * 获取id文件中资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getIdRes(Context context, String resName) {
        return getResId(context, "id", resName);
    }

    /**
     * 获取数组资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getArrayResId(Context context, String resName) {
        return getResId(context, "array", resName);
    }

    /**
     * 获取style中资源
     *
     * @param context
     * @param resName
     * @return
     */
    public static int getStyleResId(Context context, String resName) {
        return getResId(context, "style", resName);
    }

    /**
     * 获取context中对应类型的资源id
     *
     * @param context
     * @param type
     * @param resName
     * @return
     */
    private static int getResId(Context context, String type, String resName) {
        return context.getResources().getIdentifier(resName, type, context.getPackageName());
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,024评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,115评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,268评论 4 61
  • 开营三天来,组内氛围还可以,收到11份自我介绍,有几位小伙伴不够主动。 下周计划: 1.确定共读书目 2.进一步了...
    麋鹿心成长阅读 120评论 0 0