?android:attr/selectableItemBackground with another existing background

ぃ、小莉子 提交于 2019-11-29 09:19:03

问题


I have a 9patch set as the background of my layout. However I still want to provide touch feedback by using the selectableItemBackground attr.

I've tried using a <layer-list> with the 9patch and selectableItemBackground as the android:drawable of the second <item>, however that did not work.

I could also try making a selector and overlay the gradient drawable android uses for selectableItemBackground in list_selector_background_pressed.xml with a <layer-list>. But in 4.4 KitKat the selected background color is actually gray instead of blue in JellyBeans, so I can't really hardcode it :(

There has to be an easier way, right guys? D:


回答1:


I've tried using a with the 9patch and selectableItemBackground as the android:drawable of the second , however that did not work.

Yes, drawable attribute in a layer-list (or state-list) does not accept an attr value. You would see a Resource.NotFoundException. A look at LayerDrawable's (or StateListDrawable's) source code explains why: the value that you provide is assumed to be a drawable's id.

But, you can retrieve a theme and platform-specific drawable for an attribute in code:

// 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();

Now, you can create a 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);

You'll also need to set yourLayoutContainer's clickable attribute:

android:clickable="true"


来源:https://stackoverflow.com/questions/20269910/androidattr-selectableitembackground-with-another-existing-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!