안드로이드 앱에서 사용되는 리소스는 2가지로 구분된다.
기본적으로 drawable, layout, mipmap, values가 있다. 이외에도 만들어서 쓸 수 있는데
기본적으로 생성되는 것을 포함하면 아래와 같다.
디렉터리 명 | 리소스 종류 |
---|---|
animator | 속성 애니메이션 XML |
anim | 트윈 애니메이션 XML |
color | 색상 상태 목록 정의 XML |
drawable | 이미지 리소스 |
mipmap | 앱 실행 아이콘 리소스 |
layout | 레이아웃 XML |
menu | 메뉴 규성 XML |
raw | 원시 형태로 이용되는 리소스 파일 |
values | 단순값으로 이용되는 리소스 |
xml | 특정 디렉터리가 정의되지 않은 나머지 XML 파일 |
font | 글꼴 리소스 |
몇 가지 규칙이 있다.
규칙의 이유
그 이유는 리소스 디렉터리와 파일을 코드에서 그대로 사용하지 않고 R 파일에 식별자로 등록해서 사용하기 때문이다.
layout, mipmap 디렉터리는 위 표에서 명시된 것 이외의 설명은 필요하지 않기 때문에 넘어가겠다.
PNG, JPB, GIF, 9.PNG 파일들을 저장할 수 있고 XML로 작성된 이미지도 저장할 수 있다.
<shape>
: 도형을 의미. shape
속성을 이용해 도형의 타입을 지정할 수 있다.rectangle
, oval
, line
, ring
가 있고 android:shape="rectangle
로 사용<corners>
: 둥근 모서리를 그리는 데 사용. shape
값이 rectangle
일 때만 적용<gradient>
: 그라데이션 색상 지정<size>
: 도형의 크기 지정<solid>
: 도형의 색상 지정<stroke>
: 도형의 윤곽선 지정문자열, 색상, 크기, 스타일, 배열 등의 값들을 XML 파일로 저장한다.
values와 다른 디렉터리의 사용버에 차이가 있다.
다른 디렉터리
R.layout.activity_main
values 디렉터리
name
속성값이 등록된다.strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="Hello">Hello, World!</string>
</resources>
레이아웃 XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
코드에서 사용
binding.textView.text = getString(R.string.hello)
<resources>
<color name="text_color">#FFFF00</color>
</resources>
크기 리소스는 dimen
태그를 사용한다.
<resources>
<dimen name="text_size">20dp</dimen>
</resources>
레이아웃 xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"/>
코드에서 사용
binding.textView.text = getString(R.string.hello)
binding.textView.setTextColor(ResourcesCompat.getColor(resources, R.color.text_color, null))
binding.textView.textSize = resources.getDimension(R.dimen.text_size)
앱 리소스와 다르게 안드로이드 플랫폼에서 제공하는 리소스로 프로젝트 탐색 창에서 [Packages] -> [Libraries] 에서 확인할 수 있다.
플랫폼 리소스도 R 파일에 등록된 식별자로 사용할 수 있다. 앱에 있는 리소스는 아니고 android.R 플랫폼 라이브러리에 등록되어 있다.
코드에서 사용
binding.imageView.setImageDrawable(ResourcesCompat.getDrawable(resources, android.R.drawable.alert_dark_frame, null))
binding.textView.text = getString(android.R.string.emptyPhoneNumber)
XML에서 사용할 때 플랫폼 리소스는 앱 리소스와 다르게 @android:
패턴을 사용한다.
XML에서 사용
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/resource_example" />