developer tip

“? android : attr / activatedBackgroundIndicator”는 어떻게 작동합니까?

optionbox 2020. 9. 23. 07:33
반응형

“? android : attr / activatedBackgroundIndicator”는 어떻게 작동합니까?


선택 항목에 대한 상황 별 작업 표시 줄을 표시 할 때 목록에서 선택한 항목을 강조 표시하는 방법을 찾고 있었는데 내가 찾은 해결책 android:background은 내 행 레이아웃 xml 속성을 "?android:attr/activatedBackgroundIndicator".

이 설정은 어떻게 작동합니까?

  1. 관련된 메커니즘은 무엇입니까?
  2. "?", "attr", "activatedBackgroundIndicator"와 같은 구문 요소는 무엇을 의미합니까?
  3. "activatedBackgroundIndicator"의 의미는 어디에 정의되어 있습니까?

당신이 법의학 분위기에 있다면 여기에서 무슨 일이 일어나고 있는지 파고 찾는 방법이 있습니다.

android:background="?android:attr/activatedBackgroundIndicator"?

직관적으로 이것은 배경을 드로어 블로 설정하는 것을 의미합니다.

그러나 이것을 더 분해하여 신비한 드로어 블에 도달하는 방법을 살펴 보겠습니다.

정확히 말하면 "배경 속성을"activatedBackgroundIndicator "속성 이 현재 테마에서 참조하는 것으로 설정하는 것을 의미합니다 .

"현재 주제에서 언급 함"부분을 이해하면 기본적으로 커버 뒤에서 진행되는 모든 내용을 이해 한 것입니다.

기본적으로 activateBackgroundIndicator는 실제 드로어 블이 아니라 드로어 블에 대한 참조 입니다. 그렇다면 "activateBackgroundIndictor"속성은 실제로 어디에 정의되어 있습니까?

파일 이름 attrs.xml 의 sdk 디렉토리에 정의되어 있습니다 . 예를 들면 :

path_to_android_sdk / platforms / android-17 / data / res / values ​​/ attrs.xml

해당 파일을 열면 다음과 같이 선언됩니다.

<attr name="activatedBackgroundIndicator" format="reference" />

attrs.xml은 나중에보기 xml에서 사용할 모든 속성을 선언하는 곳입니다. 우리는 속성과 그 유형을 선언하고 실제로 여기에 값을 할당하지 않습니다 .

실제 값은 themes.xml에 할당됩니다 . 이 파일은 다음 위치에 있습니다.

path_to_android_sdk / platforms / android-17 / data / res / values ​​/ themes.xml

해당 파일을 열면 사용중인 테마에 따라 여러 정의가 표시됩니다 . 예를 들어 다음은 테마 이름 Theme, Theme.Light, Theme.Holo, Theme.Holo.Light에 대한 정의입니다.

<item name="activatedBackgroundIndicator">@android:drawable/activated_background</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background_light</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background_holo_dark</item>
<item name="activatedBackgroundIndicator">@android:drawable/activated_background_holo_light</item>

이제 우리는 신비한 드로어 블을 가지고 있습니다. 첫 번째 항목을 선택하면 다음 위치의 드로어 블 폴더에 정의됩니다.

path_to_android_sdk / platforms / android-17 / data / res / drawable / activated_background.xml

해당 파일을 열면 무슨 일이 일어나고 있는지 이해하는 데 중요한 드로어 블의 정의가 표시됩니다.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@android:drawable/list_selector_background_selected" />
    <item android:drawable="@color/transparent" />
</selector>

여기에서는 두 가지 상태로 드로어 블을 정의합니다. 기본 상태는 투명한 배경이고 상태가 "state_activated"이면 드로어 블은 "list_selector_background_selected"입니다.

드로어 블 및 상태에 대한 배경 정보는 이 링크참조하십시오 .

"list_selector_background_selected"는 drawable-hdpi 폴더에있는 9 패치 png 파일입니다.

이제 드로어 블 파일에 직접 연결하지 않고 ActivateBackgroundIndicator를 참조로 정의한 이유를 알 수 있습니다. 테마에 따라 올바른 드로어 블을 선택할 수 있습니다.


나는 이것도 한때 궁금했다. 많은 양의 Android 리소스가 블랙 박스처럼 보이며 직접 볼 수 없습니다. 어딘가에서 누락되었을 수 있지만 SDK 소스 코드에서 찾을 수 없습니다. 여기 내가 아는 것이 있습니다.

  • android:background 드로어 블을 가져옵니다.
  • 구문은 스타일입니다.

    "@ [+] [패키지 :] 유형 : 이름"형식의 다른 리소스에 대한 참조이거나 "? [패키지 :] [유형 :] 이름"형식의 테마 속성에 대한 참조 여야합니다.

이 경우는 ?패키지에서 테마를 보는 것을 의미 하며 이름이 android인 유형 attr입니다 activatedBackgroundIndicator.

코드 숨김에서도 액세스 할 수 있어야합니다 android.R.attr.activatedBackgroundIndicator.

Android attr속성 목록은 R.attr 에서 찾을 수 있습니다.

  • activatedBackgroundIndicator Android 3.0 이상에서 정의 된 드로어 블입니다.

    활성화 된 항목의 배경으로 사용되는 드로어 블입니다.

기본적으로 OS에 정의 된 표준 항목 일뿐입니다. Android 소스에서 찾을 수없는 것 같지만 여기에 문서 링크가 있습니다. enabledBackgroundIndicator


이것은 테마에서 값을 붙이는 형태입니다. 그 시점에서 테마 값을 알 수 없기 때문에 리소스 컴파일 중에 값을 기술적으로 알 수 없습니다. 대신 값은 (가장 일반적으로)에서 가져온 실제 테마를 기반으로 런타임에 확인됩니다 ContextThemeWrapper.

이것은 자원 값을 재사용하는 방법을 제공합니다. 여기서는 성능 측면이 아니라 조직 및 유지 관리 측면에서 말하고 있습니다. 속성은 런타임에 실제 값을 보유 할 것이라는 약속과 함께 변수처럼 작동합니다.

This approach also allows for greater customization - instead of hardcoding the value of e.g. window background drawable it gets the actual drawable from a theme, supplying a chosen attribute as the key. This lets you override the value for that attribute. You simply need to:

  1. Create your own theme (which is just a fancy name for a "style" resource), most commonly deriving from one of default themes.
  2. Supply your own value for the attribute in question.

The platform will automatically use your value provided that you have specified your theme for an activity or application. You do this like described in the question. The general syntax of theme-attribute references is described here: Referencing style attributes. You will also find an example and description of the whole mechanism there.

Edit

One thing that should be noted is the actual attribute names and their existence in various platform versions. It's fairly common for new attributes to be introduced in next platform versions - for example some were added in version 3.0 for the purpose of ActionBar styling.

You should treat attribute names as part of the API - in other words, they are part of the contract you are allowed to use. This is very similar to classes and their signatures - you use LocationManager class for the purpose of obtaining last device location because you know from some source (tutorials, reference, official guides, etc.) what's the purpose of this class. Similarly, the attribute names and their purpose are (sometimes well, sometimes miserably) defined in the Android Platform documentation.


Update: There is a more detailed version available from the API Guide so I'd like to quote it.

A style attribute resource allows you to reference the value of an attribute in the currently-applied theme. Referencing a style attribute allows you to customize the look of UI elements by styling them to match standard variations supplied by the current theme, instead of supplying a hard-coded value. Referencing a style attribute essentially says, "use the style that is defined by this attribute, in the current theme."

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:`

Original Answer:

numan salati already offered an perfect answer but it have not addressed the "?" syntax. Here's a quote from API Guide Accessing Resources

To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (@), use a question-mark (?), and the resource type portion is optional. For instance:

?[<package_name>:][<resource_type>/]<resource_name>

참고URL : https://stackoverflow.com/questions/15008150/how-does-androidattr-activatedbackgroundindicator-work

반응형