リソースID

Android用語集

カテゴリー: リソース  閲覧数:631 配信日:2013-12-18 18:08


リソースIDとは?


・プロジェクトに設置したリソースをプログラムの中から利用するため、各リソースに割り当てられた番号( int型の値 )
・リソースIDの割り当てはリソースの種類(例:drawable、layout、stringなど)によって異なる。(例:ic_launcher=0x7f020000、main=0x7f030000)
・リソースIDは「R.java」ファイルで管理されている

デフォルトで割り当てられるリソースID
・drawableクラスは「0x7f02」番台。stringクラスは「0x7f04」番台というように、予め決められている
・コード例は、デフォルトで作成された「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 e1blue.androidstyle.hello;

public final class R {
   public static final class attr {
   }
   public static final class drawable {
       public static final int ic_launcher=0x7f020000;
   }
   public static final class layout {
       public static final int main=0x7f030000;
   }
   public static final class string {
       public static final int app_name=0x7f040001;
       public static final int hello=0x7f040000;
   }
}


「R.java」ファイル
・「R.java」ファイルは初めてプロジェクトをビルドした際に作成され、その後ビルドが行われるたびに自動的に更新されていく
・そのため、ユーザーは、リソースに対するIDの管理などを行う必要はない(リソースの追加だけを行っておけばok)