zoukankan      html  css  js  c++  java
  • Android 根据字符串动态获取资源ID

    1.常用方法
    public
    int getResId(String name,Context context){ Resources r = context.getResources(); int id = r.getIdentifier(name,"drawable","com.demo"); return id; }
    2.使用反射(推荐,性能高)
    public
    class ResourceMan { public static int getResId(String variableName, Class<?> c) { try { Field idField = c.getDeclaredField(variableName); return idField.getInt(idField); } catch (Exception e) { e.printStackTrace(); return -1; } } }

    使用方法:

    int id = ResourceMan.getResId("icon",R.drawable.class);
  • 相关阅读:
    9.对话框
    8.布局管理器
    7.对象模型
    6.添加动作
    5.Qt模块简介
    4.自定义信号槽
    3.信号槽
    2.Helloworld
    1.Qt简介
    Problem E: 成绩排序
  • 原文地址:https://www.cnblogs.com/cxsy/p/7999863.html
Copyright © 2011-2022 走看看