색상 상태 목록 리소스
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
ColorStateList
는 XML로 정의할 수 있는 객체로 색상으로 적용할 수 있지만 실제로 색상이 적용되는 View
객체의 상태에 따라 색상을 변경합니다. 예를 들어, Button
위젯은 여러 상태(눌리거나 포커스가 있거나 혹은 둘 다 아닌 상태) 중 하나로 존재할 수 있는데 색상 상태 목록을 사용하면 각 상태에 다른 색상을 제공할 수 있습니다.
XML 파일에 상태 목록을 설명할 수 있습니다. 각 색상은 단일 <selector>
요소 내 <item>
요소에 정의됩니다. 각 <item>
은 다양한 속성을 사용하여 색상이 사용된 상태를 설명합니다.
각 상태가 변경되는 동안 상태 목록은 위에서 아래로 순회하고 현재 상태와 일치하는 첫 번째 항목이 사용됩니다. 선택 항목은 '가장 일치'하는 항목이 아니라 상태의 최소 기준을 충족하는 첫 번째 항목입니다.
참고: 정적 색상 리소스를 제공하려면 간단한 색상 값을 사용하세요.
- 파일 위치:
res/color/filename.xml
파일 이름은 리소스 ID로 사용됩니다.- 컴파일된 리소스 데이터 유형:
ColorStateList
을 가리키는 리소스 포인터입니다.
- 리소스 참조:
-
Java의 경우:
R.color.filename
XML의 경우: @[package:]color/filename
- 문법:
-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:lStar="floating_point_value"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
- 요소:
-
<selector>
- 필수사항. 루트 요소입니다. 하나 이상의
<item>
요소를 포함합니다.
속성:
xmlns:android
- 문자열. 필수사항. XML 네임스페이스(
"http://schemas.android.com/apk/res/android"
)를 정의합니다.
<item>
- 속성의 설명에 따라 특정 상태에서 사용할 색상을 정의합니다.
<selector>
요소의 하위 요소입니다.
속성:
android:color
- 16진수 색상. 필수사항. 색상은 RGB 값과 알파 채널(선택사항)을 사용하여 지정됩니다.
RGB 값은 항상 파운드(#
) 문자로 시작하고 알파-빨간색-녹색-파란색 정보가 이어지는 다음 형식 중 하나로 지정됩니다.
- #RGB
- #ARGB
- #RRGGBB
- #AARRGGBB
android:lStar
- 부동 소수점. 선택사항. 이 속성은 기본 색상의 인지 휘도를 수정합니다. 0과 100 사이의 부동 소수점 값 또는 이와 같이 결정되는 테마 속성을 사용합니다. 항목의 전체 색상은 기본 색상을 접근성 친화적인 색상 공간으로 변환하고 L*를
lStar
속성에 지정된 값으로 설정하여 계산됩니다.
예: android:lStar="50"
android:state_pressed
- 부울. 객체가 눌렸을 때(예: 버튼을 터치하거나 클릭한 경우) 이 항목이 사용되면
"true"
이며 기본값 즉, 버튼이 눌리지 않은 상태에서 이 항목이 사용되면 "false"
입니다.
android:state_focused
- 부울. 객체에 포커스가 있을 때(예: 트랙볼 또는 D패드를 사용하여 버튼이 강조표시된 경우) 이 항목이 사용되면
"true"
이며 기본값 즉, 포커스가 없는 상태에서 이 항목이 사용되면 "false"
입니다.
android:state_selected
- 부울. 객체가 선택되었을 때(예: 탭이 열린 경우) 이 항목이 사용되면
"true"
이며 객체가 선택되지 않았을 때 이 항목이 사용되면 "false"
입니다.
android:state_checkable
- 부울. 객체가 선택 가능할 때 이 항목이 사용되면
"true"
이며 객체가 선택 가능하지 않을 때 이 항목이 사용되면 "false"
입니다. 객체가 선택 가능한 위젯과 선택 가능하지 않은 위젯 간에 전환이 가능한 경우에만 유용합니다.
android:state_checked
- 부울. 객체가 선택되었을 때 이 항목이 사용되면
"true"
이며 객체가 선택되지 않았을 때 이 항목이 사용되면 "false"
입니다.
android:state_enabled
- 부울. 객체가 사용 설정되었을 때(터치/클릭 이벤트를 수신할 수 있음) 이 항목이 사용되면
"true"
이며 객체가 사용 중지되었을 때 이 항목이 사용되면 "false"
입니다.
android:state_window_focused
- 부울. 애플리케이션 창에 포커스가 있을 때(애플리케이션이 포그라운드에 있음) 이 항목이 사용되면
"true"
이며 애플리케이션 창에 포커스가 없을 때(예: 알림 창이 풀다운되거나 대화상자가 표시됨) 이 항목이 사용되면 "false"
입니다.
참고: 객체의 현재 상태와 일치하는 상태 목록의 첫 번째 항목이 적용됩니다. 목록의 첫 번째 항목에 위에 나와 있는 상태 속성이 포함되어 있지 않으면 이 항목이 매번 적용됩니다. 따라서 다음 예와 같이 기본값을 마지막에 배치해야 합니다.
- 예:
res/color/button_text.xml
에 저장된 XML 파일:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
다음 레이아웃 XML은 색상 목록을 View
에 적용합니다.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
- 참고 항목:
-
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Color state list resource\n\nA [ColorStateList](/reference/android/content/res/ColorStateList)\nis an object you can define in XML and apply as a color that actually changes colors depending on\nthe state of the [View](/reference/android/view/View) object it is\napplied to. For example, a [Button](/reference/android/widget/Button)\nwidget can exist in one of several states: pressed, focused, or neither. Using a color state list,\nyou can provide a different color for each state.\n\nYou describe the state list in an XML file. Each color is defined in an `\u003citem\u003e` element inside a single `\u003cselector\u003e` element. Each `\u003citem\u003e`\nuses various attributes to describe the state in which it is used.\n\nDuring each state change, the state list is traversed top to bottom, and the first item that\nmatches the current state is used. The selection is *isn't* based on the \"best\"\nmatch, but rather the first item that meets the minimum criteria of the state.\n\n**Note:** If you want to provide a static color resource, use a\nsimple [color](/guide/topics/resources/more-resources#Color) value.\n\nfile location:\n: `res/color/`*filename*`.xml` \n\n The filename is used as the resource ID.\n\ncompiled resource datatype:\n: Resource pointer to a [ColorStateList](/reference/android/content/res/ColorStateList)\n\nresource reference:\n:\n In Java: `R.color.`*filename* \n\n In XML: `@[`*package* `:]color/`*filename*\n\nsyntax:\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cselector xmlns:android=\"http://schemas.android.com/apk/res/android\" \u003e\n \u003citem\n android:color=\"hex_color\"\n android:lStar=\"floating_point_value\"\n android:state_pressed=[\"true\" | \"false\"]\n android:state_focused=[\"true\" | \"false\"]\n android:state_selected=[\"true\" | \"false\"]\n android:state_checkable=[\"true\" | \"false\"]\n android:state_checked=[\"true\" | \"false\"]\n android:state_enabled=[\"true\" | \"false\"]\n android:state_window_focused=[\"true\" | \"false\"] /\u003e\n \u003c/selector\u003e\n ```\n\nelements:\n:\n\n `\u003cselector\u003e`\n : **Required.** This is the root element. Contains one or more `\u003citem\u003e` elements.\n\n Attributes:\n\n `xmlns:android`\n : *String* . **Required.** Defines the XML namespace, which is\n `\"http://schemas.android.com/apk/res/android\"`.\n\n `\u003citem\u003e`\n : Defines a color to use during certain states, as described by its attributes. It is a\n child of a `\u003cselector\u003e` element.\n\n Attributes:\n\n `android:color`\n : *Hexadeximal color* . **Required** . The color is specified with an\n RGB value and optional alpha channel.\n\n The value always begins with a pound (`#`) character, followed by the\n Alpha-Red-Green-Blue information in one of the following formats:\n\n - #*RGB*\n - #*ARGB*\n - #*RRGGBB*\n - #*AARRGGBB*\n\n `android:lStar`\n : *Floating point* . **Optional** . This attribute modifies the base color's perceptual luminance. It takes either a\n floating-point value between 0 and 100 or a theme attribute that resolves as such. The item's\n overall color is calculated by converting the base color to an accessibility friendly color space\n and setting its L\\* to the value specified on the `lStar` attribute.\n\n Example: `android:lStar=\"50\"`\n\n `android:state_pressed`\n : *Boolean* . `\"true\"` if this item is used when the object is tapped, such as when a button\n is touched or clicked. It's `\"false\"` if this item is used in the default, non-tapped state.\n\n `android:state_focused`\n : *Boolean* . `\"true\"` if this item is used when the object is focused, such as when a button\n is highlighted using the trackball or D-pad. It's `\"false\"` if this item is used in the default,\n non-focused state.\n\n `android:state_selected`\n : *Boolean* . `\"true\"` if this item is used when the object is selected, such as when a\n tab is opened. It's `\"false\"` if this item it used when the object isn't selected.\n\n `android:state_checkable`\n : *Boolean* . `\"true\"` if this item is used when the object is checkable. It's `\"false\"` if this\n item is used when the object isn't checkable. Only useful if the object can\n transition between a checkable and non-checkable widget.\n\n `android:state_checked`\n : *Boolean* . `\"true\"` if this item is used when the object is checked. It's `\"false\"` if it\n is used when the object is deselected.\n\n `android:state_enabled`\n : *Boolean* . `\"true\"` if this item is used when the object is enabled, capable of\n receiving touch or click events. It's `\"false\"` if it is used when the object is disabled.\n\n `android:state_window_focused`\n : *Boolean* . `\"true\"` if this item is used when the application window has focus,\n meaning the\n application is in the foreground. It's `\"false\"` if this item is used when the application\n window doesn't have focus, such as if the notification shade is pulled down or a dialog appears.\n\n **Note:** The first item in the state list that\n matches the current state of the object is applied. So, if the first item in the list contains\n none of the preceding state attributes, then it applies every time. For this reason, place your\n default value last, as shown in the following example.\n\n\nexample:\n : XML file saved at `res/color/button_text.xml`: \n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cselector xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003citem android:state_pressed=\"true\"\n android:color=\"#ffff0000\"/\u003e \u003c!-- pressed --\u003e\n \u003citem android:state_focused=\"true\"\n android:color=\"#ff0000ff\"/\u003e \u003c!-- focused --\u003e\n \u003citem android:color=\"#ff000000\"/\u003e \u003c!-- default --\u003e\n \u003c/selector\u003e\n ```\n\n\n The following layout XML applies the color list to a `View`:\n\n ```xml\n \u003cButton\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"@string/button_text\"\n android:textColor=\"@color/button_text\" /\u003e\n ```\n\nsee also:\n:\n - [Color (simple value)](/guide/topics/resources/more-resources#Color)\n - [ColorStateList](/reference/android/content/res/ColorStateList)\n - [State list drawable](/guide/topics/resources/drawable-resource#StateList)"]]