Igo*_*pov 56 android android-resources
我在Google上搜索过,但找不到任何相关结果.
我还检查了官方Android文档,这个页面(所有可用资源)都是我能找到的.我在这个页面上找到的相关链接(到res/values/目录)是:
这些页面不会告诉您有关该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>
Run Code Online (Sandbox Code Playgroud)
从type属性中可以看出,它包含了通常放在目录下单独目录中的所有标准资源类型 ......res
为什么要滥用Android提供的目录并使用单个文件存储所有值?有人可以提供更多相关信息吗?
Mar*_*lts 86
文件res/values/public.xml用于将固定资源ID分配给Android资源.
请考虑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>
Run Code Online (Sandbox Code Playgroud)
在编译应用程序时,Android资产包装工具(aapt)可能会为这些资源分配以下资源ID:
public final class R {
// ...
public static final class string {
public static final int string1=0x7f040000;
public static final int string3=0x7f040001;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,将字符串资源集更改为
<?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>
Run Code Online (Sandbox Code Playgroud)
并且您会注意到资源ID @string/string3已更改:
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
}
}
Run Code Online (Sandbox Code Playgroud)
要防止这种情况,您可以使用res/values/public.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<public type="string" name="string3" id="0x7f040001" />
</resources>
Run Code Online (Sandbox Code Playgroud)
这将导致资源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
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序很少使用res/values/public.xml,因为分配给资源的资源ID无关紧要.当它们发生变化时,无论如何都会重建整个应用程序,因此将更新Java资源ID对资源的任何引用.
res/values/public.xml最重要的用户是Android平台本身.针对旧版Android构建的应用程序假定某些资源具有特定的资源ID.例如,Android资源@android:style/Theme必须始终具有资源ID 0x01030005,以使平台向后兼容针对旧版本平台构建的应用程序.
如果您对有关如何分配资源ID的更多详细信息感到好奇,请参考以下答案:android资源和资源ID之间的映射如何工作?
https://developer.android.com/studio/projects/android-library.html#PrivateResources
public.xml对于一般的库项目非常有用.如果包含public.xml,则假定其余资源都是私有的.
虽然其他项目仍然可以使用私有资源,但是linter会警告使用它们,并且它们不会包含在AndroidStudio的自动完成中.
以下是自动生成public.xml的要点 https://gist.github.com/HannahMitt/99922d4183bdb03356fd339413ba6303