zoukankan      html  css  js  c++  java
  • 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());
        }
    
    }
  • 相关阅读:
    初涉「带权并查集」&&bzoj3376: [Usaco2004 Open]Cube Stacking 方块游戏
    【树形dp】7.14城市
    【树形背包】bzoj4033: [HAOI2015]树上染色
    【计数】51nod1677 treecnt
    【树链剖分 差分】bzoj3626: [LNOI2014]LCA
    【最短路径树】51nod1443 路径和树
    python--异常处理
    常用模块之hashlib,configparser,logging模块
    面向对象之反射及内置方法
    python之封装
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/9684705.html
Copyright © 2011-2022 走看看