Android中的R类是什么? [英] What is the class R in Android?

查看:1520
本文介绍了Android中的R类是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AndroidStudio中,当我使用空活动创建项目时,我在MainActivity.java文件中获得以下代码:

In AndroidStudio, when I create a project using an empty activity, I get the following piece of code in the MainActivity.java file:

package my.company.my_proj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

,其中使用了一个名为R的隐秘类.此类R的目的是什么?

where a cryptic class named R is used. What is the purpose of this class R?

推荐答案

R是一个包含针对特定应用程序包的所有资源的定义的类.它位于应用程序包的命名空间中.

R is a class containing the definitions for all resources of a particular application package. It is in the namespace of the application package.

例如,如果您在清单中说包名称为com.foo.bar,则会生成一个R类,其中包含com.foo.bar.R中所有资源的符号.

For example, if you say in your manifest your package name is com.foo.bar, an R class is generated with the symbols of all your resources in com.foo.bar.R.

您通常要处理的两个 R

  1. android.R
  2. 中的框架资源
  3. 您自己的名称空间中的

之所以命名为R,是因为它代表 R esources,并且使人们键入更长的内容没有任何意义,尤其是因为通常在其后加上相当长的符号名,这可能会导致大量的换行器.

It is named R because that stands for Resources, and there is no point in making people type something longer, especially since it is common to end up with fairly long symbol names after it, that can cause a fair amount of line wrapper.

这篇关于Android中的R类是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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