gpt4 book ai didi

android - ?安卓 :attr/selectableItemBackground with another existing background

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:15 24 4
gpt4 key购买 nike

我将 9patch 设置为布局的背景。但是我仍然想通过使用 selectableItemBackground 来提供触摸反馈。属性。

我试过使用 <layer-list>使用 9patch 和 selectableItemBackground作为android:drawable第二个<item> ,但是那没有用。

我也可以尝试制作一个选择器并覆盖 android 用于 selectableItemBackground 的渐变可绘制对象在list_selector_background_pressed.xml<layer-list> .但是在 4.4 KitKat 中,选择的背景颜色实际上是灰色而不是 JellyBeans 中的蓝色,所以我不能真的硬编码:(

必须有更简单的方法,对吗? D:

最佳答案

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

是的,图层列表(或状态列表)中的可绘制属性不接受 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);

您还需要设置您的LayoutContainer 的可点击 属性:

android:clickable="true"

关于android - ?安卓 :attr/selectableItemBackground with another existing background,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20269910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com