android通过get方法实现查询功能,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的资源,就使用以上的方法去获取。

282b0d1c142c

项目结构.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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值