安卓如何利用资源名称获得资源Id

来源:互联网 发布:常见协议的端口号 编辑:程序博客网 时间:2024/06/05 19:50

方法1

public static int getResourceId(Context context, String resourceName, String resourceType) {    return context.getResources().getIdentifier(resourceName, resourceType,            context.getPackageName());} //Resource ID type   // public static final String ID_TYPE_STRING = "string";  //  public static final String ID_TYPE_MIPMAP = "mipmap";   // public static final String ID_TYPE_DRAWABLE = "drawable";

方法2

而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为:
String path = “com/drawable/resource/test.png.png”;
InputStream is = getClassLoader().getResourceAsStream(path);
Drawable.createFromStream(is, “src”);

方法3

放在asset文件夹里面

InputStream is = getResources().getAssets().open(“test.png”);
Drawable d = Drawable.createFromStream(is, null);

0 0
原创粉丝点击