R.java文件实际做什么以及如何 [英] what R.java file actually does and how

查看:58
本文介绍了R.java文件实际做什么以及如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个简单的android教程,在浏览项目文件夹时,我在 gen 文件夹中找到了这个 R.java 文件...

I have been working on a simple android tutorial and while browsing through the project folders I found this R.java file in gen folder...

当我打开时,在我看来就像是一团糟...

When I opened it seemed to me as a mess...

  • 第一个 R 本身是一个 class .
  • drawable id layout 等内定义了多个内部类.
  • ,并且内部类中有许多声明为如下所示的变量,并分配有十六进制值

  • first R itself is a class.
  • it had multiple Inner classes defined within eg drawable,id,layout,etc.
  • and that inner classes had lots of variables declared as below which were assigned with hex values

public static final int addr = 0x7f080003;......还有更多

public static final int addr=0x7f080003; ... ... and much more

R是自动生成的,并充当其他文件的指针

R is auto generated and acts as some pointer for other files

关于R.java的问题

Questions for R.java

  • 基本上是什么
  • 运作方式
  • 为什么
  • 值以十六进制表示
  • 在实际应用程序运行时它扮演什么角色

推荐答案

充当指向其他文件的指针"是绝对正确的,现在的问题是它指向哪些文件.

"Acts as some pointer to other files" is actually absolutely correct, now the question is which files it points to how it is done.

R文件包含项目的 res 文件夹中所有资源的ID,以及您自己定义的一些其他ID(例如,在布局中).Android资源管理系统需要这些ID才能从APK中检索文件.每个ID基本上都是一个数字,对应于资源管理系统中的某些资源.

R file contains IDs for all the resources in the res folder of your project and also some additional IDs that you define on your own (in the layouts, for example). The IDs are needed for the Android resource management system to retrieve the files from the APK. Each ID is basically a number which corresponds to some resource in the resource management system.

文件本身是必需的,因此您可以通过将资源的ID提供给资源管理器来访问或引用代码中的资源.说,如果您想在活动中设置视图,请致电

The file itself is needed so you can access or reference the resource from code by giving the ID of the resource to the resource manager. Say, if you want to set the view in the activity, you call

setContentView(R.layout.main);

R文件中的

main 包含一个数字,Android资源管理系统将其理解为称为main的布局文件.

main in the R file contains the number which is understood by the Android resource management system as the layout file which is called main.

更难对生成的字段进行错误处理.如果您不正确地输入了字段名称,则程序将无法编译,并且您会立即知道存在错误.但是,如果您输入了不正确的字符串,则应用程序在启动之前不会失败.

It's harder to make a mistake with the generated fields. If you write the field name incorrectly, your program won't compile and you will know that there's an error immediately. If you write an incorrect string, however, the application won't fail until it is launched.

如果您想了解更多有关此主题的信息,则应查看Android文档,尤其是访问资源部分.

If you want to read more on this topic, you should check the Android documentation, especially the Accessing Resources part.

这篇关于R.java文件实际做什么以及如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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