R.java

  • 3288
  • 0

摘要:R.java

Google官方文件說明:

A project's R.java file is an index into all the resources defined in the file.
You use this class in your source code as a sort of short-hand way to refer to
resources you'
ve included in your project. This is particularly powerful with the
code
-completion features of IDEs like Eclipse because it lets you quickly and
interactively locate the specific reference you're looking for.

 

The important thing to notice for now is the inner class named "layout", and
its member field "main". The Eclipse plugin noticed that you added a new XML
layout file and then regenerated this R.java file. As you add other resources to
your projects you'll see R.java change to keep up.

 

就我的理解,R.java檔相當於專案中所有資源
(屬性、圖片、id、layout (介面描述)、string (文字))的索引。
像Eclipse這類IDE可以透過R.java快速找到你所要的資源。
當你新增了專案的資源,或是修改了layout的main檔,
Eclipse plugin會發現並更新R.java。 這也就是說,你不需要也不要手動更新R.java, 我們可以見到R.java程式碼如下

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.demo.android.bmi;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int height=0x7f050000;
        public static final int result=0x7f050002;
        public static final int suggest=0x7f050003;
        public static final int weight=0x7f050001;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040000;
        public static final int bmi_btn=0x7f040003;
        public static final int bmi_height=0x7f040001;
        public static final int bmi_result=0x7f040004;
        public static final int bmi_weight=0x7f040002;
    }
}


所有的資源路徑作為R.java的inner class
而資源的位址被記錄在static屬性裡
注意
Android 中所有的資源檔案(圖片、XML等)命名都必須使用英文小寫,檔名中間只允許加上底線「」符號。其他的檔名都會造成無法正常產生R.java 檔,讓你的程式無法編譯。