0

I am very new to java and android programming. I was working through the developer page trying to figure out the goings on and the example found at this address: http://developer.android.com/training/basics/fragments/creating.html produced results that I could not explain. Specifically, the R file generates a couple of variables in the ID subpart that I could not find in any of the resource files. I don't know if I am looking in the wrong place or perhaps it pre generated not from the provided xmls? I am just trying to understand the dynamics of R file generation and interaction with the resource files and am having trouble reconciling that example.

4

2 回答 2

0

R文件中生成的ID是查不到的,是通过算法生成的,你得把它们想象成hashcode,是唯一生成的

于 2013-02-21T21:57:55.417 回答
0

在 android 项目中,R 类会在您每次构建项目时自动更新或生成。默认情况下,当您保存更改或按 ctrl + s 时,eclipse 会自动构建或编译您的代码。因此,您的资源 R 中的任何更改都会更新。当您指定新的 id、布局、drawable、菜单、字符串、样式等时,android 在您的 R 类中添加一个引用静态整数。

例子:

public static final class id {
    public static final int image=0x7f070001;
    public static final int input=0x7f070000;
    public static final int menu_settings=0x7f070002;
}

当您添加 @+id/myname R 将更新为...

public static final class id {
    public static final int image=0x7f070001;
    public static final int input=0x7f070000;
    public static final int menu_settings=0x7f070002;
    public static final int myname=0x<something>;
}

当您想要查找、设置或获取时,R 类也用于访问或作为参考。

 setContentView(R.layout.activity_main); //this will set the layout in the activity where R class points out to the integer reference in the R class which somehow used by the system to get the xml layout activity_main

 someView.setBackgroundResource(R.drawable.picture) //R points to the integer reference of image which then the system interprets the someView's background is set by the image file picture under drawable resource.

我希望这会有所帮助并欢迎使用 android :)

于 2013-02-21T22:07:18.227 回答