在Android应用程序中“R.java”的作用是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11698808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:30:05  来源:igfitidea点击:

In Android applications what is the role of "R.java"?

androidr.java-file

提问by pawarrohit14

Android applications have an automatically generated file called "R.java". Please explain its purpose.

Android 应用程序有一个自动生成的文件,称为“R.java”。请解释其目的。

E.g.

例如

  • What is contained in it?
  • What generates it?
  • 其中包含什么?
  • 是什么产生的?

Duplicate:What is the concept behind R.java?

重复:R.java 背后的概念是什么?

回答by jeet

Every application uses resources such as strings, drawbles, layouts, and styles. Instead of hard coding such resources into an application, one externalizes them and refers to them by ID. The R.java file contains all those resource IDs, whether assigned by the programmer or generated by the sdk.

每个应用程序都使用诸如字符串、绘图、布局和样式之类的资源。不是将这些资源硬编码到应用程序中,而是将它们外部化并通过 ID 引用它们。R.java 文件包含所有这些资源 ID,无论是由程序员分配的还是由 sdk 生成的。

回答by iTurki

R.java is where you have access to anything you have in your resources:

R.java 是您可以访问资源中任何内容的地方:

  • Drawables
  • Layouts
  • Strings
  • Arrays
  • ....etc
  • 可绘制对象
  • 布局
  • 字符串
  • 数组
  • ....等等

You can read more here.

您可以在此处阅读更多内容。

回答by Chirag

R.java is autogenerated on build. It's content is based on the resource files (including layouts and preferences).

When you delete it, it gets recreated, but if you create your own, you will get into trouble as the build system will not be able to replace it.

R.java 在构建时自动生成。它的内容基于资源文件(包括布局和首选项)。

当您删除它时,它会被重新创建,但是如果您创建自己的,则会遇到麻烦,因为构建系统将无法替换它。