java android开发中R.layout.activity_main的含义(JAVA语言)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32649447/
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-11-02 20:33:38  来源:igfitidea点击:

Meaning of R.layout.activity_main in android development (JAVA language)

javaandroid

提问by Shreyans

What is the meaning of R.layout.activity_main ?

R.layout.activity_main 是什么意思?

I understand that "." operator is used to define variables of a particular object but in this case its been used twice so I can't make anything out of it. Also what exactly is "R" and "layout"?

我明白那个 ”。” 运算符用于定义特定对象的变量,但在这种情况下它被使用了两次,所以我无法从中做出任何事情。“R”和“布局”究竟是什么?

I mean obviously they are classes (right?) but what is their function ? Basically explain R.layout.activity_main !

我的意思是显然它们是类(对吗?)但它们的功能是什么?基本上解释 R.layout.activity_main !

Please comment if question too vague or too broad.

如果问题太模糊或太宽泛,请发表评论。

回答by Punksmurf

R.javais a class (with inner classes, like layoutor string) generated during the build process with references to your app's resources. Every resource you create (or which is provided by Android) is referenced by an integer in R, called a resource id.

R.java是在构建过程中生成的一个类(带有内部类,如layoutstring),并引用您的应用程序资源。您创建的(或由 Android 提供的)每个资源都由 中的一个整数引用R,称为资源 ID。

R.layout.*references any layout resource you have created, usually in /res/layout. So if you created an activity layout called activity_main.xml, you can then use the reference in R.layout.activity_mainto access it. Many built-in functionality readily accepts such a resource id, for example setContentView(int layoutResid)which you use during the creation of your activity and where you probably encountered this particular example.

R.layout.*引用您创建的任何布局资源,通常在/res/layout. 因此,如果您创建了一个名为 的活动布局activity_main.xml,则可以使用引用R.layout.activity_main来访问它。许多内置功能很容易接受这样的资源 ID,例如setContentView(int layoutResid)您在创建 Activity 期间使用的资源 ID,以及您可能遇到此特定示例的位置。

If you create a string resource (in strings.xml) like this:

如果您像这样创建一个字符串资源(在 strings.xml 中):

<string name="app_name">Application name</string>

it will get a new reference in R.string.app_name. You can then use this everywhere where a string resource is accepted, for example the android:labelfor your application in AndroidManifest.xml, or on a TextView; either in the xml:

它将在R.string.app_name. 然后,您可以在接受字符串资源的任何地方使用它,例如在android:labelAndroidManifest.xml或在 TextView 上的应用程序;无论是在 xml 中:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    />

or in code: textview.setText(R.string.app_name).

或在代码中:textview.setText(R.string.app_name)

You can access resources programmatically using the Resourcesclass, to which you can get a reference by calling getResourceson any context (like your activity). So for example you can get your app name described above in your activity by calling this.getResources().getString(R.string.app_name).

您可以使用Resources该类以编程方式访问资源,您可以通过调用getResources任何上下文(例如您的活动)来获取对该类的引用。例如,您可以通过调用在您的活动中获取上述应用程序名称this.getResources().getString(R.string.app_name)

You can also supply different resources for different device properties/settings (like screen size or language), which you can access using the same references in R. The easiest example here, imho, is strings: if you add a new valuesfolder in /reswith a language specifier (so /res/values-nlfor Dutch) and you add strings with the same identifier but a different translation and the resource management system cleverly figures out which one to provide for you based on your user's device.

您还可以为不同的设备属性/设置(如屏幕尺寸或语言)提供不同的资源,您可以使用R. 这里最简单的例子,恕我直言,是字符串:如果您添加一个带有语言说明符的新values文件夹/res/res/values-nl荷兰语也是如此),并且您添加具有相同标识符但翻译不同的字符串,资源管理系统会巧妙地找出要提供的字符串根据您用户的设备为您服务。

I hope this helps a bit. For more information on resources see the documentation.

我希望这能有所帮助。有关资源的更多信息,请参阅文档

回答by Martin C.

In Android Ris an Java-class that is auto-generated from your resources by the build process.

在 Android 中R是一个 Java 类,它是由构建过程从您的资源自动生成的。

The R.layoutmember is a auto-generated class that contains all IDs for layouts.

R.layout成员是一个自动生成的类,其中包含布局的所有 ID。

R.layout.activity_mainis a static final intmember that represents the ID of the layout-file in layout/activity_main.xml.

R.layout.activity_main是一个static final int成员,表示布局文件的 ID layout/activity_main.xml

回答by Dani?l van den Berg

Okay, so R is a generated class. If you're lucky enough you'll never see it nor have to touch it, otherwise you did something very wrong.

好的,所以 R 是一个生成的类。如果你足够幸运,你将永远不会看到它,也不必触摸它,否则你做错了什么。

When you make a layout, or any change to a layout, Android Studio generates quite a couple files for you. This includes a R.java file. Here's a piece of an R.java class:

当您进行布局或对布局进行任何更改时,Android Studio 会为您生成相当多的文件。这包括一个 R.java 文件。这是 R.java 类的一部分:

public final class R {
    public static final class anim {
        public static final int abc_fade_in = 0x7f050000;
        public static final int abc_fade_out = 0x7f050001;
        public static final int abc_grow_fade_in_from_bottom = 0x7f050002;
        public static final int abc_popup_enter = 0x7f050003;
        public static final int abc_popup_exit = 0x7f050004;
        public static final int abc_shrink_fade_out_from_bottom = 0x7f050005;
        public static final int abc_slide_in_bottom = 0x7f050006;
        public static final int abc_slide_in_top = 0x7f050007;
        public static final int abc_slide_out_bottom = 0x7f050008;
        public static final int abc_slide_out_top = 0x7f050009;
    }
    public static final class attr {
        public static final int actionBarDivider = 0x7f010062;
        public static final int actionBarItemBackground = 0x7f010063;
        public static final int actionBarPopupTheme = 0x7f01005c;
        public static final int actionBarSize = 0x7f010061;
        public static final int actionBarSplitStyle = 0x7f01005e;
        public static final int actionBarStyle = 0x7f01005d;
        public static final int actionBarTabBarStyle = 0x7f010058;
        public static final int actionBarTabStyle = 0x7f010057;
        public static final int actionBarTabTextStyle = 0x7f010059;

As you can see, in this case if I'd type

如您所见,在这种情况下,如果我输入

R.anim.abc_fade_in

I'd be selecting the value 0x7f050000;. Every layout file is mapped out in this R file, and gets an ID by which android recognizes it. The layouts are located in R.Layout. So, R.layout.activity_main gets you the value of variable activity_main of the class layout of the class R.

我会选择值 0x7f050000;。每个布局文件都映射到这个 R 文件中,并获得一个 android 识别它的 ID。布局位于 R.Layout 中。因此,R.layout.activity_main 为您获取类 R 的类布局的变量 activity_main 的值。

And again, don't try finding or changing your generated R file. Things can go very wrong if you do that.

同样,不要尝试查找或更改生成的 R 文件。如果你这样做,事情可能会变得很糟糕。

回答by Blackbelt

Ris an auto-generated class, and describe the resources of your project. It contains static inner classes. layoutis one of them. R.layout refers to the inner class called layout. activity_mainis a public static finalmember of the class layout

R是一个自动生成的类,并描述您项目的资源。它包含静态内部类。layout是其中之一。R.layout 指的是称为布局的内部类。activity_mainpublic static final班级的成员layout

回答by Jas

From https://stackoverflow.com/a/4953282/1393766

来自https://stackoverflow.com/a/4953282/1393766

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

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

If you want to inflate a layout inside your activity class,you can use R.layout.activity_main where layout specifies that your resource is a layout and it's name is activity_main.

如果你想在你的活动类中扩展一个布局,你可以使用 R.layout.activity_main 其中 layout 指定你的资源是一个布局,它的名字是activity_main。

If you want to use a drawable image in a layout inside your activity class,you can use R.drawable.image_name where drawable specifies that your resource is a drawable image.

如果你想在你的活动类的布局中使用一个可绘制的图像,你可以使用 R.drawable.image_name 其中 drawable 指定你的资源是一个可绘制的图像。

Also,R.java class is an autogenerated class which is not supposed to alter manually.

此外,R.java 类是一个自动生成的类,不应手动更改。