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

后端 未结 5 1953
清酒与你
清酒与你 2020-12-31 09:25

What is the meaning of R.layout.activity_main ?

I understand that \".\" operator is used to define variables of a particular object but in this case its been used t

相关标签:
5条回答
  • 2020-12-31 09:57

    R.java is a class (with inner classes, like layout or 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.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_main to 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.

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

    <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:label for your application in AndroidManifest.xml, or on a TextView; either in the 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).

    You can access resources programmatically using the Resources class, to which you can get a reference by calling getResources on 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).

    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 values folder in /res with a language specifier (so /res/values-nl for 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.

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

    0 讨论(0)
  • 2020-12-31 09:57

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

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

    R.layout.activity_main is a static final int member that represents the ID of the layout-file in layout/activity_main.xml.

    0 讨论(0)
  • 2020-12-31 10:02

    R is an auto-generated class, and describe the resources of your project. It contains static inner classes. layout is one of them. R.layout refers to the inner class called layout. activity_main is a public static final member of the class layout

    0 讨论(0)
  • 2020-12-31 10:08

    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.

    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:

    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.

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

    0 讨论(0)
  • 2020-12-31 10:19

    From 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.

    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.

    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.

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

    0 讨论(0)
提交回复
热议问题