选择可绘制文件路径 [英] Select Drawable file path

查看:149
本文介绍了选择可绘制文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了关于接收来自服务器的事件数据的Andr​​oid应用程序。

在一个ListView我把名称和事件和图像的类别。
我的问题是:每个类别应该有一个特定的画面。如何选择这在绘制文件夹中的照片应显示?

 视图V; / **我从参数这一观点。** /
ImageView的iconCategory =(ImageView的)诉.findViewById(R.id.category_icon);
可绘制可绘制= NULL;
如果(o.getCategoria()。等于(类别1)){
/ *这里我想设置的图标,文件名为icon_category1.png。我想要做的,通过实例绘制* /
}否则如果(o.getCategoria()。等于(组别)){
/ *这里我想设置的图标来命名icon_category2.png我想文件做到这一点通过实例绘制* /
}否则如果(o.getCategoria()。等于(类别3)){
/ *这里我想设置的图标来命名icon_category3.png我想文件做到这一点通过实例绘制* /
}
iconCategory.setImageDrawable(绘制);


解决方案

您可以做

 可绘制D = getResources()getDrawable(R.drawable.icon_category2)。
iconCategory.setImageDrawable(四);

的文件名的第一部分是可拉伸的id

I am developing an android application that receives data about events from a server.

In a ListView I put the name and category of the event and a picture. My problem is: each category should have a specific picture. How do I choose which of the pictures in the drawable folder should be displayed?

View v; /**I get this view from the parameters.**/
ImageView iconCategory = (ImageView) v .findViewById(R.id.category_icon);
Drawable drawable = null;
if (o.getCategoria().equals("category1")) {
/* here I want to set the icon to the file named icon_category1.png . I wanna do that by instantiating drawable */
} else if (o.getCategoria().equals("category2")) {
/* here I want to set the icon to the file named icon_category2.png I wanna do that by instantiating drawable */
} else if (o.getCategoria().equals("category3")) {
/* here I want to set the icon to the file named icon_category3.png I wanna do that by instantiating drawable */
}
iconCategory.setImageDrawable(drawable);

解决方案

You can do

Drawable d = getResources().getDrawable(R.drawable.icon_category2);
iconCategory.setImageDrawable(d);

The first part of the filename is the drawable id.

这篇关于选择可绘制文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆