亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android 開發技巧 - 通過getIdentifier()方法實現在Module中使用Application資源

標簽:
Android

前言

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

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



作者:chenyk
链接:https://www.jianshu.com/p/282b0d1c142c


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消