我有一个 9patch 设置为布局的背景。但是我仍然想通过使用提供触摸反馈selectableItemBackground
attr.
我尝试过使用<layer-list>
与 9patch 和selectableItemBackground
as the android:drawable
第二个的<item>
,但是这没有用。
我还可以尝试制作一个选择器并覆盖渐变可绘制 android 用途selectableItemBackground
in list_selector_background_pressed.xml
with a <layer-list>
。但在 4.4 KitKat 中,选定的背景颜色实际上是灰色而不是 JellyBeans 中的蓝色,所以我无法真正对其进行硬编码:(
一定有一个更简单的方法,对吧伙计们? d:
我尝试过使用 9patch 和
selectableItemBackground 作为第二个的 android:drawable ,
但这没有用。
是的,图层列表(或状态列表)中的可绘制属性不接受attr
价值。你会看到一个Resource.NotFoundException
。查看 LayerDrawable(或 StateListDrawable)的源代码可以解释原因:您提供的值被假定为可绘制对象的 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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)