Android 通过名称获取资源ID
当我们获取网络数据的时候,解析之后往往都是一个字符串,而不是资源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的更多相关文章
- [Android学习笔记]使用getIdentifier()获取资源Id
使用getIdentifier()获取资源Id Android中可以使用getIdentifier()获取资源ID ex: 根据图片名称获取图片Id private int getImageResId ...
- 【我的Android进阶之旅】Android使用getIdentifier()方法根据资源名来获取资源id
有时候我们想动态的根据一个资源名获得到对应的资源id,就可以使用getResources().getIdentifier()方法来获取该id.然后再使用该id进行相关的操作. 1.Demo示例 下面用 ...
- Android-使用getIdentifier()获取资源Id
使用getIdentifier()获取资源Id int i= getResources().getIdentifier("icon", "drawable", ...
- Android_使用getIdentifier()获取资源Id
Android 获取资源ID的另外一种方法,常规获取ID是在特定的文件夹下面的资源,如果在比较特殊的文件夹下面,就需要其他方法获取ID 了: 使用getIdentifier()方法可以方便的获各应用包 ...
- Android中打包JAR时获取资源ID的方法
前言:在打包android源码的时,有的时候源码中包含了资源文件,但是jar包中不包含,所以会异常,解决的方案就是不用系统的提供的id名,而是直接 获取id,如反射. 1.系统提供的方法: /** * ...
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
在将自己写的工具打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射机制动态的获取资源. /** * 反射得到组件的id号 */ public static ...
- getIdentifier()获取资源Id
工作需要使用getIdentifier()方法可以方便的获各应用包下的指定资源ID.主要有两种方法:(1)方式一Resources resources = context.getResources() ...
- Android 通过资源名,获取资源ID
有时候我们知道一个图片的文件名,我们需要知道在R文件中,该资源的ID,使用如下方法: public static int getIdByName(Context context, String cla ...
- Android中通过反射获取资源Id
package com.cp.utils; import android.content.Context; public class CPResourceUtil { public static in ...
随机推荐
- 史上最全Vim快捷键键位图 -- 入门到进阶
文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/linux/all-vim-cheatshe ...
- shell输出颜色
#!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:- echo -e "\033[30m 黑色字 \033[0m" echo -e "\033 ...
- FineUI 获取x_state并解析
public JObject PostBackStates() { JObject xState = null; if (Page.IsPostBack) { string state = HttpC ...
- CStdioFile.WriteString无法向文件写入中文
CStdioFile.WriteString向文件中写入字符串,但字符串中带有中文的,无法写入. 解决方案: 将带有中文的字符串进行转换后再写入文件. char* pBuffer = NULL; lo ...
- 隐藏显示终端的光标(shell echo,linux c printf)
https://www.cnblogs.com/niocai/archive/2011/11/11/2245727.html 一.使用shell 的 echo 命令实现. echo -ne <c ...
- HTTP 错误 500.21 - Internal Server Error 解决方案(转)
不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“Ni ...
- Response 和 Request
1. request 对象和 response 对象均由服务器创建. 2. 服务器处理请求的流程: 服务器每次收到请求时, 都会为这个请求开辟一个新的线程; 服务器会把客户端的请求数据封装到 requ ...
- Leetcode 之 Set Mismatch
645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...
- tomcat访问管理页面出现:403 Access Denied 解决方法
点击红色框框出现以下403错误 打开context.xml文件 vim /usr/local/tomcat/webapps/manager/META-INF/context.xml <Conte ...
- Deeplearning——动态图 vs. 静态图
动态图 vs. 静态图 在 fast.ai,我们在选择框架时优先考虑程序员编程的便捷性(能更方便地进行调试和更直观地设计),而不是框架所能带来的模型加速能力.这也正是我们选择 PyTorch 的理由, ...