我在 Google 上搜索过,但找不到任何相关结果。
我还查看了官方 Android 文档 和 this page (对于所有可用的资源)是我能找到的。我在此页面上找到的相关链接(到 res/values/
目录)是:
这些页面没有说明 res/values/public.xml
文件的任何信息。
这是我为 this type of file 找到的示例 .
小 fragment
<?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。
考虑 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 Assets 打包工具 (aapt) 可能会在编译应用时为这些资源分配以下资源 ID:
public final class R {
// ...
public static final class string {
public static final int string1=0x7f040000;
public static final int string3=0x7f040001;
}
}
现在,将字符串资源集更改为
<?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 已更改:
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:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<public type="string" name="string3" id="0x7f040001" />
</resources>
这将导致资源 ID 按如下方式分配:
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 对资源的任何引用都将被更新。
res/values/public.xml 最重要的用户是 Android 平台本身。针对旧版本 Android 构建的应用程序假定某些资源具有某个资源 ID。例如,Android 资源 @android:style/Theme
平台必须始终具有资源 ID 0x01030005,才能向后兼容针对旧版本平台构建的应用。
如果您想了解更多有关如何分配资源 ID 的详细信息,请参阅此答案:How does the mapping between android resources and resources ID work?
关于android - Android 上的 res/values/public.xml 文件有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9348614/