为什么android创建R.java文件
为什么不将数据( strings.xml、main.xml )留在 .xml 文件中?为什么要把它转换成R.java?
看来android可以读取xml(例如androidmanifest.xml)。那么为什么不将 strins.xml 数据和 main.xml 数据保留为 .xml 形式并将这些 xml 发送到 Android 设备呢?
我是新人......也许我最终会发现 R.java 中除了直接从那些 .xml 派生的静态变量之外还有更多内容。
谢谢, 香农
why not just leave the data( strings.xml, main.xml ) in the .xml files? why convert it into the R.java?
it seems that android can read xml ( eg. androidmanifest.xml ). so why not just leave the strins.xml data and main.xml data in .xml form and send those xml's to the android device?
I am new ... maybe I will eventually see that there is a lot more in the R.java other than mere static variables that derived directly from those .xml's.
thanks,
Shannon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这样您就可以轻松引用在布局、可绘制和字符串文件中创建的对象。此类代表可以在代码中实例化或可以在编码中使用的所有资源
so that you can easily reference you object that you have created in you layout, drawable and string files. this class represents all the resources that can be instantiated inside a code or can be used inside your coding
我认为,这样您就可以使用 R 类对象轻松地从代码中引用资源 ID,避免编译错误
So that you can easily reference your resources ids from your code with the R class object avoiding compile errors, I think
如果您的程序中存在错误,您希望尽早检测到它。
如果资源以 XML 形式保留,则任何错误都将在运行时被发现(测试驱动或用户驱动)。测试永远无法证明不存在所有缺陷,因此软件的用户可能会发现一些错误!
但是,如果资源在 R 中转换为源代码,则编译器将检查对它们的所有引用。任何错误(与资源引用相关)都会在更早的阶段被检测到,早在您的客户看到它们之前。
If you have a bug in your program, you want it detected as early as possible.
If resources are retained as XML any bug will be found at runtime (either test-driven or user-driven). Testing can never prove the absence of all defects, so some bugs may be found by the users of your software!
However, if resources are converted to source in
R
then all references to them will be checked by the compiler. Any bugs (relating to resource references) are detected at this much earlier stage, long before your customers get to seem them.将 XML 内容的 id 引用传输到代码中可以在编码时轻松使用这些引用,并且在编译时可以看到正确的用法。因此可以避免运行时异常。
我不知道这是否是核心原因,但这对我来说很重要:-)。
Transferring the id references of the XML content into code makes it easy to use these references while coding and the correct usage is visible during compile time. So runtime exceptions are avoided.
I do not know if this is the core reason but that's what counts for me :-).
R.java 是所有资源的处理程序。
您创建资源,R.java 会自动在您的代码和资源之间建立连接。您可以轻松地从 R 获取各种类型的资源,并且 android 提供了大量支持 R.java 及其处理程序(那些整数)的 API。您不需要手动打开res文件的流,进行文件读取、图像解码等操作,非常方便。
R.java is a handler to all the resources.
You create your resources, and the R.java automatically build a connection between your code and your res. You can easily fetch your various kind of resources from R, and android provide lots of API supporting R.java and its handlers(those ints). You don
t need to manually open a stream to res files, do file reading, images decoding and etc. It
s great convience.R.java
不包含任何数据。它是由aapt
在构建过程中准备资源以将其打包到 APK 中时生成的,包括以更易于阅读和解析的格式编译 XML 文件。 R.java 包含静态 int 成员,它们只是打包资源的索引。请参阅详细了解构建过程,以及查看项目的
bin/
文件夹。R.java
doesn't contain any data. It's generated byaapt
during the build process when it prepares resources to pack them into the APK, including compiling XML files in a format that's easier to read and parse.R.java
contains static int members that are only indexes to the packed resources.See A Detailed Look at the Build Process, and also take a look into a project's
bin/
folder.