通过资源文件名称获取资源id
方式一:
int identifier1 = getResources().getIdentifier("com.outdoors.jinghuang.demo:drawable/ic_smart_update", null, null);
参数解释:第一个参数:包名:资源类型/资源名
方式二:
int identifier2 = getResources().getIdentifier("off", "drawable", getPackageName());
参数解释:
第一个参数:资源名,
第二个参数:资源类型,
第三个参数:包名
代码示例
package com.outdoors.jinghuang.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by jing.huang on 2016/12/11.
*/
public class GetIdentiferMethodDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.identifer_demo);
ImageView showPicture1 = (ImageView) findViewById(R.id.show_picture1);
ImageView showPicture2 = (ImageView) findViewById(R.id.show_picture2);
//包名:资源文件名/资源名
int identifier1 = getResources().getIdentifier("com.outdoors.jinghuang.demo:drawable/ic_smart_update", null, null);
if (identifier1 <= 0) {
Toast.makeText(this, "未获取到该资源,请重试", Toast.LENGTH_SHORT).show();
} else {
showPicture1.setImageResource(identifier1);
}
//资源名,资源属性,包名
int identifier2 = getResources().getIdentifier("off", "drawable", getPackageName());
if (identifier2 <= 0) {
Toast.makeText(this, "未获取到该资源,请重试", Toast.LENGTH_SHORT).show();
} else {
showPicture2.setImageResource(identifier2);
TextView contentTv = (TextView) findViewById(identifier3);
Toast.makeText(this, "TextView的内容为" + contentTv.getText(), Toast.LENGTH_SHORT).show();
}
方式一:
int identifier1 = getResources().getIdentifier("com.outdoors.jinghuang.demo:drawable/ic_smart_update", null, null);
参数解释:第一个参数:包名:资源类型/资源名
方式二:
int identifier2 = getResources().getIdentifier("off", "drawable", getPackageName());
参数解释:
第一个参数:资源名,
第二个参数:资源类型,
第三个参数:包名
代码示例
package com.outdoors.jinghuang.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by jing.huang on 2016/12/11.
*/
public class GetIdentiferMethodDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.identifer_demo);
ImageView showPicture1 = (ImageView) findViewById(R.id.show_picture1);
ImageView showPicture2 = (ImageView) findViewById(R.id.show_picture2);
//包名:资源文件名/资源名
int identifier1 = getResources().getIdentifier("com.outdoors.jinghuang.demo:drawable/ic_smart_update", null, null);
if (identifier1 <= 0) {
Toast.makeText(this, "未获取到该资源,请重试", Toast.LENGTH_SHORT).show();
} else {
showPicture1.setImageResource(identifier1);
}
//资源名,资源属性,包名
int identifier2 = getResources().getIdentifier("off", "drawable", getPackageName());
if (identifier2 <= 0) {
Toast.makeText(this, "未获取到该资源,请重试", Toast.LENGTH_SHORT).show();
} else {
showPicture2.setImageResource(identifier2);
}
//资源类型为id的示例
int identifier3 = getResources().getIdentifier("content_tv", "id", getPackageName());TextView contentTv = (TextView) findViewById(identifier3);
Toast.makeText(this, "TextView的内容为" + contentTv.getText(), Toast.LENGTH_SHORT).show();
}
}
资源存放目录