프로그래밍을 하다보면 R.layout...라는 구문을 많이 쓰게된다. 그렇다면 R은 무엇일까?
R은 R.java로 작성되어있는 정적 클래스의 정적 변수를 가르킨다.
R클래스는 안드로이드 빌드 시스템에 의해 자동으로 생성되는 자바 클래스이며,
이 클래스는 리소스를 효율적으로 접근할 수 있도록 리소스를 integer 값으로 관리하는 역할을 한다.
각 파일을 추가할때마다 R에 자동으로 저장이된다.
또한 정적파일이기때문에 어디서든지 사용이 가능하다.
R은 이곳에 위치하고있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public final class R { public static final class attr { } public static final class dimen { public static final int activity_horizontal_margin=0x7f050000; public static final int activity_vertical_margin=0x7f050001; } public static final class id { public static final int action_settings=0x7f080000; } public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int menu_main=0x7f070000; } public static final class mipmap { public static final int ic_launcher=0x7f020000; } public static final class string { public static final int action_settings=0x7f060000; public static final int app_name=0x7f060001; public static final int hello_world=0x7f060002; } public static final class style { /** Customize your theme here. */ public static final int AppTheme=0x7f040000; } } | cs |
또한 R의 내용을 보면 이렇다.
그리고 자바코드에서는 리소스 자원을 R.mipmap.icon, R.layout.main, R.string.app_name 처럼 접근할 수 있으며
XML에서는 @mipmap/icon, @string/hello로 접근 할 수 있다.
참고로 R을 인식하지 못해서 빌드 오류가 생길때가 있는데, 그때는 빌드클린을 하고 다시 빌드를하면된다.
출처 : https://kairo96.gitbooks.io/android/content/ch2.4.html
'Programming > Android' 카테고리의 다른 글
View (0) | 2016.09.07 |
---|---|
Service (0) | 2016.09.05 |
안드로이드의 구성요소 요약 (0) | 2016.09.05 |
Thread (백그라운드에서 UI변경) (0) | 2016.09.03 |
Toolbar (0) | 2016.09.03 |