R.xxx.xxx 资源 ID 在各个版本中都是静态的吗?
如果我要将字符串资源 id 存储在数据库中,当我将其拉回几个版本时,我是否可以期望资源 id 仍将我指向所需的字符串?
示例案例:
存储资源 ID
0x7f060003
,假设它指向 对于不确定的字符串 "I'm a pleasure string!" 存储资源时间到了,我需要资源,但是X版本已经过去了,资源id还会指向“我是一个愉快的字符串!”?
If I were to store a string resource id in a database, could I expect the resource id to still point me to the desired string when I pull it back a few versions down the line?
Sample Case:
Store the recource Id
0x7f060003
, with the assumption it points
to the string "I'm a pleasant string!" for an inderterminate
amount of time.Time comes along in which I need the resource, but X versions have passed, will the resource id still point to "I'm a pleasant string!"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 getResources().getIdentifier() 代替。这样,即使其 id 发生变化,您也可以通过名称检索资源。下面是一个示例:
然后您可以仅将资源名称保存在数据库中,并稍后从数据库中检索资源。
请参阅此 链接了解更多信息。
Use getResources().getIdentifier() instead. This way you can retrieve your ressource by its name, even if its id changes. Here's an example:
You can then save just the name of the ressource in the database, and retrieve the ressources later from the database.
See this link for more info.
当你说X版本已经通过时,你的意思是你已经改变了你的源代码吗?并更新了apk?
R id 整数是在编译时创建的。因此,一旦构建,您的应用程序就会保持不变。
但是,如果您添加新的布局/id/drawable,您的 ID将会发生变化。
When you say X versions have passed, do you mean you have changed your source code? And updated the apk?
The R id integers are created at compile time. Therefore constant in your app once built.
However if you add a new layout / id / drawable your ID's will change.
您可以通过在 res/values 目录中创建 public.xml 来修复资源 ID。
这是我用过的一个:
You can fix resource ids by creating a public.xml in the res/values directory.
Here is one I have used:
最好的解决方案是在代码中创建图标的映射
String 是您可以保存在数据库中的密钥(可绘制资源),甚至后端 API 响应
Int 中的密钥也是可绘制资源
The best solution would be to create a map of your icons in your code
String is your key (to that drawable resource) that you can keep in your database, or even the key from backend API response
Int is the resource drawable