也就是通过res文件夹下的资源名称获取R文件里面生成的id, 这个id用起来和在代码中使用R.XXX.XXX一样.

获取方法为:

    getResources().getIdentifier(String name, String defType, String defPackage)

上图上代码

@Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_setImage:                int imageId = getResources().getIdentifier("image", "drawable", getPackageName());                mImage.setImageResource(imageId);                break;            case R.id.btn_setText:                int textId = getResources().getIdentifier("text", "string", getPackageName());                mBtnSetText.setText(textId);                break;            case R.id.btn_setColor:                int colorId = getResources().getIdentifier("colorPrimary", "color", getPackageName());                mBtnSetColor.setTextColor(getResources().getColor(colorId));                break;            default:                break;        }    }


Parameters

name The name of the desired resource.
defType Optional default resource type to find, if “type/” is not included in the name. Can be null to require an explicit type.
defPackage Optional default package to find, if “package:” is not included in the name. Can be null to require an explicit package.

Returns

int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

更多相关文章

  1. Android手机铃声代码实现
  2. Android(安卓)ERROR: Unable to resolve dependency for
  3. android DDMS 连接真机(己ROOT),用file explore看不到data/data文
  4. Android反射机制实现与原理
  5. Android(安卓)EditText设置Filter以后,xml布局文件中maxLength不
  6. android ListView显示网络图片
  7. Android(安卓)studio APP开发 控制UI布局的方式,用代码书写UI界面
  8. Android欢迎界面,一个Activity搞定
  9. 定制Android关机界面

随机推荐

  1. Android开发_多线程详解(九)
  2. android中获取设备的型号和手机厂商
  3. Android(安卓)UI设计 ViewPager 中嵌套Ho
  4. Android实现倒计时跳转Activity
  5. android:EditText控件imeOptions属性
  6. ionic android打包
  7. 收到短信了,我该做点什么?
  8. Android地图尝试之两行代码调用高德地图
  9. dumpsys的使用笔记
  10. MAC中Android(安卓)Studio快捷键(一)