2013-11-28 4 views
7

내 레이아웃의 배경으로 9 패치 세트가 있습니다. 그러나 나는 여전히 selectableItemBackground attr을 사용하여 터치 피드백을 제공하려고합니다.? android : attr/selectableItemBackground 다른 기존 배경으로

나는 작동하지 않았다하지만, 두 번째 <item>android:drawable로 a를 9patch와 <layer-list>selectableItemBackground를 사용하여 시도했습니다.

selectableItemBackground에 의 <layer-list>에있는 그라데이션 드로어 블로딩 드로어 드로잉 드로어를 오버레이하여 시도해 볼 수도 있습니다. 그러나 4.4 킷캣에서 선택한 배경 색상은 실제로 대신 젤리 빈 파란색 회색, 그래서 난 정말이있다

가 더 쉬운 방법이 바로들 D를 :(하드 코딩 할 수 없습니다?

답변

16

을 나는 9patch 및 안드로이드 등 selectableItemBackground와를 사용하여 시도했다 : 두 번째의 당김을 그러나 그것은 작동하지 않았다

예, 당김 레이어 목록에서 속성 (또는 국가 목록)한다. attr 값을 허용하지 않습니다.가 표시됩니다.. LayerDrawable (또는 StateListDrawable) 소스 코드를 살펴보면 그 이유를 알 수 있습니다. 제공하는 값은 drawable의 ID로 간주됩니다.

, 당신은 코드의 속성에 대한 테마와 플랫폼 별 당김 검색 할 수 있습니다 : 이제

// Attribute array 
int[] attrs = new int[] { android.R.attr.selectableItemBackground }; 

TypedArray a = getTheme().obtainStyledAttributes(attrs); 

// Drawable held by attribute 'selectableItemBackground' is at index '0'   
Drawable d = a.getDrawable(0); 

a.recycle(); 

을 만들 수있는 LayerDrawable :

LayerDrawable ld = new LayerDrawable(new Drawable[] { 

         // Nine Path Drawable 
         getResources().getDrawable(R.drawable.Your_Nine_Path), 

         // Drawable from attribute 
         d }); 

// Set the background to 'ld' 
yourLayoutContainer.setBackground(ld); 

당신은 또한에 필요합니다 yourLayoutContainer's clickable 속성을 설정하십시오 :

android:clickable="true" 
+0

재미있는 것 같습니다. 시도 할 것이다. –

+0

@JasonHu 죄송합니다. 전 전화로 귀하의 의견을 완전히 듣지 않았습니다. 질문이 있으면 계속하십시오. – Vikram

+0

놀라운. 매력처럼 작동합니다. 처음에는 선택한 상태에 대해 선택기를 만들어야한다고 생각했습니다 (기본에서는 9patch, 선택하면 LayerDrawable에서만). 하지만 당신이 selectableItemBackground에서 가져올 drawable은 실제로 선택자 그 자체입니다. 고맙습니다. –