资源文件帮助类ResourceHelper
2017-06-12 16:20
190 查看
public class ResourceHelper {
private ResourceHelper() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
private static final String RES_ID = "id";
private static final String RES_STRING = "string";
private static final String RES_DRABLE = "drable";
private static final String RES_LAYOUT = "layout";
private static final String RES_STYLE = "style";
private static final String RES_COLOR = "color";
private static final String RES_DIMEN = "dimen";
private static final String RES_ANIM = "anim";
private static final String RES_MENU = "menu";
/**
* @return 获取上下文对象
*/
public static Context
93df
getContext() {
return App.getContext();
}
/**
*
* @return 获取资源Android资源
*/
public static Resources getResource(){
return getContext().getResources();
}
/**
*
* @param resId
* @return
*/
public static String getString(int resId){
return getResource().getString(resId);
}
/**
*
* @param resId
* @return
*/
public static Drawable getDrawable(int resId){
return getResource().getDrawable(resId);
}
/**
*
* @param resId
* @return
*/
public static int getColor(int resId){
return getResource().getColor(resId);
}
/**
*
* @param resId
* @return
*/
public static float getDimens(int resId){
return getResource().getDimension(resId);
}
/**
* 获取资源文件的id
*
* @param resName
* @return
*/
public static int getId(String resName) {
return getResId(resName, RES_ID);
}
/**
* 获取资源文件string的id
*
* @param resName
* @return
*/
public static int getStringId(String resName) {
return getResId(resName, RES_STRING);
}
/**
* 获取资源文件drable的id
*
* @param resName
* @return
*/
public static int getDrableId(String resName) {
return getResId(resName, RES_DRABLE);
}
/**
* 获取资源文件layout的id
*
* @param resName
* @return
*/
public static int getLayoutId(String resName) {
return getResId(resName, RES_LAYOUT);
}
/**
* 获取资源文件style的id
*
* @param resName
* @return
*/
public static int getStyleId(String resName) {
return getResId(resName, RES_STYLE);
}
/**
* 获取资源文件color的id
*
* @param resName
* @return
*/
public static int getColorId(String resName) {
return getResId(resName, RES_COLOR);
}
/**
* 获取资源文件dimen的id
*
* @param resName
* @return
*/
public static int getDimenId(String resName) {
return getResId(resName, RES_DIMEN);
}
/**
* 获取资源文件ainm的id
*
* @param resName
* @return
*/
public static int getAnimId(String resName) {
return getResId(resName, RES_ANIM);
}
/**
* 获取资源文件menu的id
*
* @param resName
*/
public static int getMenuId(String resName) {
return getResId(resName, RES_MENU);
}
/**
* 获取资源文件ID
*
* @param resName
* @param defType
* @return
*/
public static int getResId(String resName, String defType) {
return getResource().getIdentifier(resName, defType, getContext().getPackageName());
}
}
private ResourceHelper() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
private static final String RES_ID = "id";
private static final String RES_STRING = "string";
private static final String RES_DRABLE = "drable";
private static final String RES_LAYOUT = "layout";
private static final String RES_STYLE = "style";
private static final String RES_COLOR = "color";
private static final String RES_DIMEN = "dimen";
private static final String RES_ANIM = "anim";
private static final String RES_MENU = "menu";
/**
* @return 获取上下文对象
*/
public static Context
93df
getContext() {
return App.getContext();
}
/**
*
* @return 获取资源Android资源
*/
public static Resources getResource(){
return getContext().getResources();
}
/**
*
* @param resId
* @return
*/
public static String getString(int resId){
return getResource().getString(resId);
}
/**
*
* @param resId
* @return
*/
public static Drawable getDrawable(int resId){
return getResource().getDrawable(resId);
}
/**
*
* @param resId
* @return
*/
public static int getColor(int resId){
return getResource().getColor(resId);
}
/**
*
* @param resId
* @return
*/
public static float getDimens(int resId){
return getResource().getDimension(resId);
}
/**
* 获取资源文件的id
*
* @param resName
* @return
*/
public static int getId(String resName) {
return getResId(resName, RES_ID);
}
/**
* 获取资源文件string的id
*
* @param resName
* @return
*/
public static int getStringId(String resName) {
return getResId(resName, RES_STRING);
}
/**
* 获取资源文件drable的id
*
* @param resName
* @return
*/
public static int getDrableId(String resName) {
return getResId(resName, RES_DRABLE);
}
/**
* 获取资源文件layout的id
*
* @param resName
* @return
*/
public static int getLayoutId(String resName) {
return getResId(resName, RES_LAYOUT);
}
/**
* 获取资源文件style的id
*
* @param resName
* @return
*/
public static int getStyleId(String resName) {
return getResId(resName, RES_STYLE);
}
/**
* 获取资源文件color的id
*
* @param resName
* @return
*/
public static int getColorId(String resName) {
return getResId(resName, RES_COLOR);
}
/**
* 获取资源文件dimen的id
*
* @param resName
* @return
*/
public static int getDimenId(String resName) {
return getResId(resName, RES_DIMEN);
}
/**
* 获取资源文件ainm的id
*
* @param resName
* @return
*/
public static int getAnimId(String resName) {
return getResId(resName, RES_ANIM);
}
/**
* 获取资源文件menu的id
*
* @param resName
*/
public static int getMenuId(String resName) {
return getResId(resName, RES_MENU);
}
/**
* 获取资源文件ID
*
* @param resName
* @param defType
* @return
*/
public static int getResId(String resName, String defType) {
return getResource().getIdentifier(resName, defType, getContext().getPackageName());
}
}
相关文章推荐
- some times naive:为什么不把资源文件全部打包到resource文件里
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- android.content.res.Resources$NotFoundException: String resource ID #0x0 找不到资源文件ID #0x0
- build-helper-maven-plugin 配置多 source resource 文件
- wp7读取项目生成的资源(Resource)文件
- 代码阅读总结之Fitch and Mather 7.0(资源文件Resource随笔)
- C# 使用资源文件 Resource.resx 的方法
- 解决GetManifestResourceNames()无法读取资源文件
- 利用WebResource属性实现资源文件自包含于控件
- 根据文件名称获取资源文件路径 pathForResource
- C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- 对资源文件(Resource)的重复读写
- Watch your resource(注意你的资源文件)
- 多语言资源文件帮助
- 导出resource文件的的资源
- 学习使用资源文件[4] - 用资源中的图片做背景、使用 LoadFromResourceID
- 关于用GetManifestResourceNames读不出资源文件的解决
- [转]C# DLL资源文件打包(图片、JS、CSS)[WebResource]
- Delphi之使用资源文件(Using Resource Files)
- Java 资源文件路径详解(getResource)