Android 如何将字符串转换为 Drawable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21856260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert String to Drawable
提问by Utku Soyta?
I have many icon in drawable folder and I have their name as String. How can I access to drawable folder and change background imageView (or any view) use these name in dynamically. Thanks
我在 drawable 文件夹中有很多图标,它们的名字是 String。如何访问可绘制文件夹并更改背景图像视图(或任何视图)动态使用这些名称。谢谢
回答by FD_
This can be done using reflection:
这可以使用反射来完成:
String name = "your_drawable";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier()
:
或使用Resources.getIdentifier()
:
String name = "your_drawable";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
Then use this for setting the drawable in either case:
然后在这两种情况下使用它来设置可绘制对象:
view.setBackground(drawable)
回答by M. Bongini
int resId = getResources().getIdentifier("your_drawable_name","drawable",YourActivity.this.getPackageName());
Drawable d = YourActivity.this.getResources().getDrawable(resId);
回答by nikis
It can be done like this:
可以这样做:
ImageView imageView = new ImageView(this);
imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));
回答by Phant?maxx
Try this:
尝试这个:
public Bitmap getPic (int number)
{
return
BitmapFactory.decodeResource
(
getResources(), getResourceID("myImage_" + number, "drawable", getApplicationContext())
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
回答by stamanuel
if you have the filename as string you can use:
如果您将文件名作为字符串,则可以使用:
int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
with this id you can access it like always (assumed its a drawable):
使用此 ID,您可以像往常一样访问它(假设它是可绘制的):
Drawable drawable = getResources().getDrawable(id);
回答by juan fernandez
use case if not in any activity, using @FD_ examples
用例如果不在任何活动中,使用@FD_ 示例
Note:
笔记:
if you are not in any activity you have to send context param in order to use "getResources()" or "getPackageName()", and "getDrawable(id)" is deprecated, use getDrawer(int id, Theme theme) instead. (Theme can be null):
如果您不在任何活动中,您必须发送上下文参数才能使用“getResources()”或“getPackageName()”,并且不推荐使用“getDrawable(id)”,请改用 getDrawer(int id, Theme theme)。(主题可以为空):
String name = "your_drawable";
int id = context.getResources().getIdentifier(name, "drawable",
context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id, null);