12

:색상 상태 목록은 그리기 모양에 인식되지 내가 그릴 수 <code>my_background_drawable.xml</code> 다음과 같은 정의합니다

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape android:gravity="center" 
      android:shape="rectangle"> 
      <solid android:color="@color/color_stateful" /> 
     </shape> 
    </item> 

    <item android:drawable="@drawable/selector_png_drawable" /> 
</layer-list> 

그리고 나는 또한 색상 상태 목록 자원 color_stateful.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="#FF00ff00"/> 
    <item android:color="#FFff0000"/> 
</selector> 

내가 배경으로 my_background_drawable를 주어진 일부보기의 경우 내 모양에 대해 color_stateful.xml에 정의 된 색상의 변화를 관찰 할 수 없으며 뷰 상태는 실제로 변경됩니다 (selector_png_drawable.xml은 표시기 임).

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<!-- This doesn't work 
    <item> 
     <shape android:gravity="center" 
      android:shape="rectangle"> 
      <solid android:color="@color/color_stateful" /> 
     </shape> 
    </item> 
--> 
    <item> 
     <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_pressed="true"> 
       <shape android:gravity="center" 
        android:shape="rectangle"> 
        <solid android:color="#FF00ff00" /> 
       </shape> 
      </item> 

      <item> 
       <shape android:gravity="center" 
        android:shape="rectangle"> 
        <solid android:color="#FFff0000" /> 
       </shape> 
      </item> 
     </selector> 
    </item> 

    <item android:drawable="@drawable/selector_png_drawable"" /> 
</layer-list> 

그래서 색 상태 정보가 ColorStateList 자원이 ShapeDrawable 내에서 사용하는 경우 단지 분실 또는 내가 그 일을하고 있다는 것이 사실이다 : 나는 다음과 같은 방법으로 내 my_background_drawable.xml을 수정할 때

그러나 모든 것이 괜찮습니다 잘못된?

답변

25

ColorStateList<solid>의 속성으로 XML 정의에서 전달할 수 없으며 실제로는 <shape>의 모든 속성으로 전달할 수 없습니다. 이 속성은 XML에서 색상 자원으로 비정상적으로 팽창 한 다음 Drawable의 setColor() 메소드로 전달됩니다.이 메소드는 단일 ARGB 값만 사용합니다.

상태를 기반으로 여러 항목을 포함하고 표시하는 Drawable 인스턴스 유형은 일 때 얻을 수있는 것 인 StateListDrawable입니다. 다른 모든 Drawable 인스턴스는 단순히이 컬렉션의 멤버이거나 독립 실행 형으로 그려지는 인스턴스입니다.

팽창 된 <shape> 항목은 실제로 GradientDrawable이고 ShapeDrawable이 아니라는 점에 유의하십시오. GradientDrawablein the sourceinflate() 메소드를 체크 아웃하면 각 속성의 사용 방법에 대한 모든 세부 정보를 얻을 수 있습니다.

HTH! 당신이 짠하고 있습니다

+7

Android Lollipop의 경우 더 이상 true가 아니며 'ColorStateList'가 올바르게 구문 분석됩니다. – keyboardr

+1

Lollipop에서 작동하는지 확인할 수 있습니다. <21 테스트 기기가 제대로 작동하지 않는 이유가 무엇인지 정말 혼란스러워했습니다! – Tom

+7

pre-lollipop 장치의 명백한 (그리고 끔찍한) 대안은 각 '''color' 속성에 의해서만 다른 상태 인만큼 많은' '을 생성하고 원래의' '드로어 블을 각각의 ''을 참조하는'StateListDrawable '. 그 물건의 극악은''요소 (하드 코딩 된 값 없음) 내부의 리소스 참조만을 사용함으로써 약간 완화 될 수 있으며 원래의 ''은'drawable-v21' 디렉토리에 보관 될 수 있습니다 (가능하면 완전히 나중에 전환). _blargh_ – desseim

-4

는 .... 단지 대체이

android:color="@color/color_stateful" 

android:background="@color/color_stateful" 

업데이트의 다음 my_background_drawable.xml에 프로그램 코드에서

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape android:gravity="center" 
      android:shape="rectangle"> 
      <solid android:background="@color/color_stateful" /> <!--this is the chanage i made... here--> 
     </shape> 
    </item> 

    <item android:drawable="@drawable/selector_png_drawable" /> 
</layer-list> 
+1

모양 드로어 블에 'android : background'와 같은 속성이 없습니다. –

2

사실 당신은 ColorStateListshape ->GradientDrawable의 xml 안에 단색으로 지정할 수 있지만 그 값은 new feature in Lollipop뿐입니다.

Older versions of GradientDrawable은 색상 자원 만 허용합니다.

현재 관심있는 경우 compat 대안을 사용 중입니다.