R.xxx.xxx 资源 ID 在各个版本中都是静态的吗?

发布于 2024-12-21 15:26:44 字数 264 浏览 0 评论 0原文

如果我要将字符串资源 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一梦浮鱼 2024-12-28 15:26:44

使用 getResources().getIdentifier() 代替。这样,即使其 id 发生变化,您也可以通过名称检索资源。下面是一个示例:

int resID = getResources().getIdentifier("nameofthedrawable", "drawable", "com.your.project");

然后您可以仅将资源名称保存在数据库中,并稍后从数据库中检索资源。

请参阅此 链接了解更多信息。

Use getResources().getIdentifier() instead. This way you can retrieve your ressource by its name, even if its id changes. Here's an example:

int resID = getResources().getIdentifier("nameofthedrawable", "drawable", "com.your.project");

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.

明媚如初 2024-12-28 15:26:44

当你说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.

且行且努力 2024-12-28 15:26:44

您可以通过在 res/values 目录中创建 public.xml 来修复资源 ID。
这是我用过的一个:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public type="drawable" name="button" id="0x7f020000" />
    <public type="drawable" name="button_flat" id="0x7f020001" />
    <public type="drawable" name="button_flat_square" id="0x7f020002" />
    <public type="drawable" name="button_flat_vert" id="0x7f020003" />
    <public type="drawable" name="button_grey" id="0x7f020004" />
    <public type="drawable" name="right" id="0x7f020005" />
    <public type="drawable" name="left" id="0x7f020006" />
    <public type="drawable" name="up" id="0x7f020007" />
    <public type="drawable" name="down" id="0x7f020008" />
    <public type="drawable" name="red" id="0x7f020009" />
    <public type="drawable" name="green" id="0x7f02000A" />
    <public type="drawable" name="yellow" id="0x7f02000B" />
    <public type="drawable" name="blue" id="0x7f02000C" />
    <public type="drawable" name="plus" id="0x7f020010" />
    <public type="drawable" name="minus" id="0x7f020011" />
</resources>

You can fix resource ids by creating a public.xml in the res/values directory.
Here is one I have used:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public type="drawable" name="button" id="0x7f020000" />
    <public type="drawable" name="button_flat" id="0x7f020001" />
    <public type="drawable" name="button_flat_square" id="0x7f020002" />
    <public type="drawable" name="button_flat_vert" id="0x7f020003" />
    <public type="drawable" name="button_grey" id="0x7f020004" />
    <public type="drawable" name="right" id="0x7f020005" />
    <public type="drawable" name="left" id="0x7f020006" />
    <public type="drawable" name="up" id="0x7f020007" />
    <public type="drawable" name="down" id="0x7f020008" />
    <public type="drawable" name="red" id="0x7f020009" />
    <public type="drawable" name="green" id="0x7f02000A" />
    <public type="drawable" name="yellow" id="0x7f02000B" />
    <public type="drawable" name="blue" id="0x7f02000C" />
    <public type="drawable" name="plus" id="0x7f020010" />
    <public type="drawable" name="minus" id="0x7f020011" />
</resources>
眼眸印温柔 2024-12-28 15:26:44

最好的解决方案是在代码中创建图标的映射

val iconMap: HashMap<String, Int>

String 是您可以保存在数据库中的密钥(可绘制资源),甚至后端 API 响应

Int 中的密钥也是可绘制资源

iconMap.put("cat", R.drawable.ic_cat)
iconMap.getOrDefault("dog", R.drawable.empty)

The best solution would be to create a map of your icons in your code

val iconMap: HashMap<String, Int>

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

iconMap.put("cat", R.drawable.ic_cat)
iconMap.getOrDefault("dog", R.drawable.empty)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文