Android 上的 res/values/public.xml 文件有什么用? [英] What is the use of the res/values/public.xml file on Android?

查看:43
本文介绍了Android 上的 res/values/public.xml 文件有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google 上搜索过,但找不到任何相关结果.

I've searched on Google, but couldn't find any relevant results.

我还查看了官方 Android 文档这个页面(所有可用资源)是我能找到的全部.我在此页面上找到的相关链接(到 res/values/ 目录)是:

I also checked the official Android docs and this page (for all the available resources) was all I could find. The relevant links (to the res/values/ directory) I found on this page were:

这些页面没有说明 res/values/public.xml 文件的任何信息.
这是我为这种类型的文件找到的示例.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public type="attr" name="commentTextColor" id="0xAA010007" />
    <public type="drawable" name="add_icon_bl" id="0xAA020000" />
    <public type="layout" name="act_date_picker" id="0xAA030001" />
    <public type="anim" name="activity_slide_from_bottom" id="0xAA040000" />
    <public type="xml" name="pref_menu" id="0xAA050000" />
    <public type="raw" name="alert_bell_animation_bl" id="0xAA060000" />
    <public type="array" name="taskRepeat" id="0xAA070000" />
    <public type="color" name="theme_main_color_bl" id="0xAA080000" />
    <public type="string" name="no_internet" id="0xAA0a0001" />
    <public type="id" name="page1" id="0xAA0d0015" />
</resources>

正如您从 type 属性中看到的,它几乎包含您通常放在 中的所有标准资源类型res 目录下的单独目录...

为什么要滥用 Android 提供的目录并使用单个文件来存储所有值?有人可以提供更多相关信息吗?

推荐答案

res/values/public.xml文件用于为Android资源分配固定的资源ID.

The file res/values/public.xml is used to assign fixed resource IDs to Android resources.

考虑 res/values/strings.xml 中的这些字符串资源集:

Consider these set of string resources in res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="string1">String 1</string>
    <string name="string3">String 3</string>
</resources>

Android 资产打包工具 (aapt) 可能会在编译应用程序时为这些资源分配以下资源 ID:

The Android Asset Packaging Tool (aapt) might assign the following resource IDs for these resources when the app is compiled:

public final class R {
    // ...
    public static final class string {
        public static final int string1=0x7f040000;
        public static final int string3=0x7f040001;
    }
}

现在,将字符串资源集更改为

Now, change the set of string resources to

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="string1">String 1</string>
    <string name="string2">String 2</string>
    <string name="string3">String 3</string>
</resources>

并且您会注意到 @string/string3 的资源 ID 已更改:

and you'll notice that the resource ID for @string/string3 has changed:

public final class R {
    // ...
    public static final class string {
        public static final int string1=0x7f040000;
        public static final int string2=0x7f040001;
        public static final int string3=0x7f040002; // New ID! Was 0x7f040001
    }
}

为了防止这种情况,您可以使用 res/values/public.xml:

To prevent this, you can use res/values/public.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public type="string" name="string3" id="0x7f040001" />
</resources>

这将导致资源 ID 被分配如下:

which will result in the resource IDs being assigned as follows:

public final class R {
    // ...
    public static final class string {
        public static final int string1=0x7f040000;
        public static final int string2=0x7f040002;
        public static final int string3=0x7f040001; // Resource ID from public.xml
    }
}

应用程序很少使用 res/values/public.xml,因为分配给资源的资源 ID 无关紧要.当它们发生变化时,无论如何都会重新构建整个应用程序,因此 Java 代码中通过资源 ID 对资源的任何引用都将更新.

Applications rarely have any use for res/values/public.xml since the resource IDs assigned to resources does not matter. When they change, the entire application is rebuilt anyway so any references in Java code to resources by resource ID will be updated.

res/values/public.xml 最重要的用户是 Android 平台本身.针对旧版 Android 构建的应用程序假定特定资源具有特定资源 ID.例如,Android 资源 @android:style/Theme 必须始终具有资源 ID 0x01030005,以便平台向后兼容针对旧版本平台构建的应用.

The most significant user of res/values/public.xml is the Android platform itself. Applications built against old versions of Android assumes that certain resource have a certain resource ID. For example, the Android resource @android:style/Theme must always have the resource ID 0x01030005 for the platform to be backwards compatible with apps built against old versions of the platform.

如果您对如何分配资源 ID 的更多详细信息感到好奇,请参阅此答案:android资源和资源ID的映射是如何工作的?

If you are curious about more details on how resource IDs are assigned, please refer to this answer: How does the mapping between android resources and resources ID work?

这篇关于Android 上的 res/values/public.xml 文件有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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