Android:drawable id更改后资源修改 [英] Android: drawable id change after resources modify

查看:887
本文介绍了Android:drawable id更改后资源修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,使用图库组件来选择一个图标,并将其分配给按钮。
图标集位于res / drawable文件夹中,可以使用指南的典型适配器访问库:
private Integer [] Imgid = {
R.drawable.icon_home,
R.drawable.icon_home2,
...
}
选择图标后,我将设置存储在带有按钮和drawable的id的数据库中。
所有的工作,但我注意到,如果我想要添加或修改我的应用程序的一般的图标集或资源,旧的资源的id可能会改变,所以db的首选项是指错误图标。
是否有办法强制使用R类的旧ID,以便将来不会更改?还是有另一种方法来管理组件厨房的项目与id的另一个属性? (可能是字符串名称和反射?)

i've an android application that use a gallery component to choose an icon and assign it to a button. The icon set is located in res/drawable folder and is accessed in the gallery with the typical adapter of the guide: private Integer[] Imgid = { R.drawable.icon_home, R.drawable.icon_home2, ... } After an icon choosing, i stored the settings in a db with id of the button and of the drawable. All works done, but i've noticed that if i'll want to add or modify my icon set or resources in general of my application, ids of old resource could change, so the preferences in the db refers to wrong icon. Is there a way to force the old id of R class so they didn't change in the future? Or is there another way to manage the item of the component galley with another attribute than id? (maybe string name and reflection?)

提前感谢

推荐答案

如果您不打算改变它,您可以在数据库中存储drawable的名称。 getResourceEntryName 从资源ID返回名称:

You can store the name of the drawable in the database if you don't plan to change that. getResourceEntryName returns the name from the resource id:

Resources resources = getResources();
String name = resources.getResourceEntryName(R.drawable.icon);

您可以从 getIdentifier

And you can get back the resource id from the name with getIdentifier:

int resId = resources.getIdentifier(name, "drawable", "your.package.name");

这篇关于Android:drawable id更改后资源修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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