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

后端 未结 5 1990
清酒与你
清酒与你 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 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.

提交回复
热议问题