当我们获取网络数据的时候,解析之后往往都是一个字符串,而不是资源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());
} }

Android 通过名称获取资源ID的更多相关文章

  1. [Android学习笔记]使用getIdentifier()获取资源Id

    使用getIdentifier()获取资源Id Android中可以使用getIdentifier()获取资源ID ex: 根据图片名称获取图片Id private int getImageResId ...

  2. 【我的Android进阶之旅】Android使用getIdentifier()方法根据资源名来获取资源id

    有时候我们想动态的根据一个资源名获得到对应的资源id,就可以使用getResources().getIdentifier()方法来获取该id.然后再使用该id进行相关的操作. 1.Demo示例 下面用 ...

  3. Android-使用getIdentifier()获取资源Id

    使用getIdentifier()获取资源Id int i= getResources().getIdentifier("icon", "drawable", ...

  4. Android_使用getIdentifier()获取资源Id

    Android 获取资源ID的另外一种方法,常规获取ID是在特定的文件夹下面的资源,如果在比较特殊的文件夹下面,就需要其他方法获取ID 了: 使用getIdentifier()方法可以方便的获各应用包 ...

  5. Android中打包JAR时获取资源ID的方法

    前言:在打包android源码的时,有的时候源码中包含了资源文件,但是jar包中不包含,所以会异常,解决的方案就是不用系统的提供的id名,而是直接 获取id,如反射. 1.系统提供的方法: /** * ...

  6. Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)

    在将自己写的工具打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源. /** * 反射得到组件的id号 */ public static ...

  7. getIdentifier()获取资源Id

    工作需要使用getIdentifier()方法可以方便的获各应用包下的指定资源ID.主要有两种方法:(1)方式一Resources resources = context.getResources() ...

  8. Android 通过资源名,获取资源ID

    有时候我们知道一个图片的文件名,我们需要知道在R文件中,该资源的ID,使用如下方法: public static int getIdByName(Context context, String cla ...

  9. Android中通过反射获取资源Id

    package com.cp.utils; import android.content.Context; public class CPResourceUtil { public static in ...

随机推荐

  1. 网络编程3 网络编程之缓冲区&subprocess&粘包&粘包解决方案

    1.sub简单使用 2.粘包现象(1) 3.粘包现象(2) 4.粘包现象解决方案 5.struct学习 6.粘包现象升级版解决方案 7.打印进度条

  2. Powershell & SQL

    一.使用Powershell插入数据到SQL中 1.方法一 $ServerName=xxx $Database=xxx $UserID=xxx $Pwd=xxx $conn=new-object Sy ...

  3. docker在团队中的实践 How To Install Docker In CentOS

    " 预发布机器(centos-6.5),给每个同学都开通了ssh这个机器是大家一起共用的,稍后导些数据下来.后续 项目上线,产品测试,都是在这上面进行.  目前在一个物理机 " 3 ...

  4. 转!!Java设置session超时(失效)的时间

    Java设置session超时(失效)的时间   在一般系统登录后,都会设置一个当前session失效的时间,以确保在用户长时间不与服务器交互,自动退出登录,销毁session具体设置的方法有三种:1 ...

  5. python引入同一目录下的py文件

    python引入同一目录下的py文件 注意:python2和python3的包内import语法有区别,下面介绍一下python3的包内import语法 例如在admin.py文件中要引入dealco ...

  6. 我的Android进阶之旅------>android如何将List请求参数列表转换为json格式

    本文同步发表在简书,链接:http://www.jianshu.com/p/395a4c8b05b9 前言 由于接收原来的老项目并进行维护,之前的http请求是使用Apache Jakarta Com ...

  7. Codeforces Round#251(Div 2)D Devu and his Brother

    --你以为你以为的.就是你以为的? --有时候还真是 题目链接:http://codeforces.com/contest/439/problem/D 题意大概就是要求第一个数组的最小值要不小于第二个 ...

  8. SVG Use(转)

    转自:http://www.zhangxinxu.com/wordpress/2014/07/introduce-svg-sprite-technology/ 未来必热:SVG Sprite技术介绍 ...

  9. Django web 框架

    目录 与Django的第一次见面 安装.文件解释与基本命令 Settings Models Views 路由系统 模板 Form表单 Cookie与Session CSRF防护

  10. likely(x)与unlikely(x) __builtin_expect

    本文讲的likely()和unlikely()两个宏,在linux内核代码和一些应用中可常见到它们的身影.实质上,这两个宏是关于GCC编译器内置宏__builtin_expect的使用. 顾名思义,l ...