?机器人:ATTR / selectableItemBackground与另一个现有背景 [英] ?android:attr/selectableItemBackground with another existing background
问题描述
我有一个9patch设置为我的布局的背景。不过,我还是想要通过使用 selectableItemBackground
ATTR提供触摸反馈。
我用尝试了<层列表>
与9patch和 selectableItemBackground
为机器人:可绘制
的第二<项目>
,但没有奏效。
我也可以尝试做一个选择和覆盖梯度绘制机器人在 list_selector_background_ pressed.xml $ C使用了
selectableItemBackground
$ C>与<层列表>
。但在4.4奇巧选择背景色实际上是灰色的,而不是蓝牌的软糖,让我可以在c它不是真的很难$ C $:(
必须有一个更简单的方法,正确的人? D:
我试着使用与9patch已经和 selectableItemBackground由于Android:第二次的绘制, 然而,没有工作。
是的,在一层列表绘制属性(或国家列表)不接受 ATTR
值。你会看到一个 Resource.NotFoundException
。一看LayerDrawable(或StateListDrawable的)源$ C $ C解释了原因:您提供的价值被假定为一个绘制的ID。
不过,您可以检索主题,特定于平台的可绘制在code的属性:
//属性阵列
INT [] ATTRS =新INT [] {android.R.attr.selectableItemBackground};
TypedArray A = getTheme()obtainStyledAttributes(ATTRS)。
//绘制对象按属性举办selectableItemBackground'是索引'0'
绘制对象D = a.getDrawable(0);
a.recycle();
现在,您可以创建一个 LayerDrawable
:
LayerDrawable LD =新LayerDrawable(新绘制对象[] {
//九路径绘制对象
getResources()。getDrawable(R.drawable.Your_Nine_Path)
//从属性绘制对象
D});
//将背景设置为LD
yourLayoutContainer.setBackground(LD);
您还将需要设置 yourLayoutContainer的可点击
属性:
机器人:可点击=真
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:
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"
这篇关于?机器人:ATTR / selectableItemBackground与另一个现有背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!