当我们获取网络数据的时候,解析之后往往都是一个字符串,而不是资源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. iPhone设备分辨率一览

    地址:https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/

  2. 更新设置api

    8.8 更新设置 API Elasticsearch允许在elasticsearch.yml文件中指定各种参数来调优.但你应该把这个文件当做 默认设置,可以在运行时通过Elasticsearch RE ...

  3. 关于websocket

    一句话总结: websocket可以说是基于HTTP但有有所进化的一个介于应用层和传输层的接口抽象,不是协议. 1 需要基于HTTP进行3次握手,4次挥手(在握手期间建立websocket连接,不再通 ...

  4. session补充

  5. Tomcat WEB站点部署

    上线的代码有两种方式, 第一种方式是直接将程序目录放在webapps目录下面 第二种方式是使用开发工具将程序打包成war包,然后上传到webapps目录下面.下面让我们见识一下这种方式 这个网站里面已 ...

  6. Android Wear - Design Principles for Android Wear(设计原则)

    ---------------------------------------------------------------------------------------------------- ...

  7. Mysql学习笔记—视图

    1.什么是视图 视图(View)是一种虚拟存在的表.其内容与真实的表相似,包含一系列带有名称的列和行数据.但是视图并不在数据库中以存储的数据的形式存在.行和列的数据来自定义视图时查询所引用的基本表,并 ...

  8. 关于\r和\n的区别

    回车和换行来源 在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两 ...

  9. gdb各种调试命令和技巧

    陈皓:用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在UNIX平台 ...

  10. appium之Context切换

    原文地址http://blog.csdn.net/Mandypan/article/details/51396302 Context切换driver.getContextHandles();//获取页 ...