Android 通过名称获取资源ID
2024-08-29 11:05:20
当我们获取网络数据的时候,解析之后往往都是一个字符串,而不是资源id,所有我们没有办法直接使用,只能通过名称来获取到资源id,
package com.example.administrator.demo;
import android.content.Context;
/**
* Created by Administrator on 2017/8/27 0027.
*/
public class GetResourcesUtils{
/**
* 获取资源文件的id
*
* @param context
* @param resName
* @return
*/
public static int getId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "id", context.getPackageName());
}
/**
* 获取资源文件中string的id
*
* @param context
* @param resName
* @return
*/
public static int getStringId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "string", context.getPackageName());
}
/**
* 获取资源文件drable的id
*
* @param context
* @param resName
* @return
*/
public static int getDrableId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "drable", context.getPackageName());
}
/**
* 获取资源文件layout的id
*
* @param context
* @param resName
* @return
*/
public static int getLayoutId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "layout", context.getPackageName());
}
/**
* 获取资源文件style的id
*
* @param context
* @param resName
* @return
*/
public static int getStyleId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "style", context.getPackageName());
}
/**
* 获取资源文件color的id
*
* @param context
* @param resName
* @return
*/
public static int getColorId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "color", context.getPackageName());
}
/**
* 获取资源文件dimen的id
*
* @param context
* @param resName
* @return
*/
public static int getDimenId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "dimen", context.getPackageName());
}
/**
* 获取资源文件ainm的id
*
* @param context
* @param resName
* @return
*/
public static int getAnimId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "anim", context.getPackageName());
}
/**
* 获取资源文件menu的id
*/
public static int getMenuId(Context context, String resName) {
return context.getResources().getIdentifier(resName, "menu", context.getPackageName());
}
}
最新文章
- [NOIP2012]国王游戏 题解
- 二模 (7) day2
- 【HTML5】炫丽的时钟效果Canvas绘图与动画基础练习
- java实现音频转换
- Linux学习系列之Linux入门(二)Vim学习
- 开发腾讯移动游戏平台SDK ios版Ane扩展 总结
- tomcat与resin的比较
- 【MFC学习笔记-作业8-蝴蝶飞~】【什么鬼作业】
- [转载]给10万pv的WordPress选择最便宜高可用的硬件以及WordPress高并发支持
- css中的media
- 剖析html对标准标签和自定义标签闭合与不闭合渲染问题
- iframe中的模态框遮罩父窗口原理
- MySql数据库第一天
- RPM包的版本号比较
- Linux 搜索日志信息
- Only one complex type allowed as argument to a web api controller action.
- Js参数值中含有单引号或双引号解决办法
- iphone6主板注释
- Linux系统 Centos6 安装
- python数据字典的操作