Jas*_* Hu 8 android nine-patch android-selector
我有一个9patch设置作为我的布局的背景.但是我仍然想通过使用selectableItemBackgroundattr 提供触摸反馈.
我已经使用试图<layer-list>与9patch和selectableItemBackground作为android:drawable第二的<item>,但没有奏效.
我也可以尝试进行选择和覆盖梯度绘制Android使用了selectableItemBackground在list_selector_background_pressed.xml同一个<layer-list>.但是在4.4 KitKat中,选择的背景颜色实际上是灰色而不是JellyBeans中的蓝色,所以我不能真正硬编码:(
合适的方式,对吧?d:
Vik*_*ram 16
我已经尝试使用9patch和selectableItemBackground作为第二个的android:drawable,但是这不起作用.
是的,图层列表(或状态列表)中的drawable属性不接受attr值.你会看到一个Resource.NotFoundException.查看LayerDrawable(或StateListDrawable)的源代码可以解释原因:您提供的值假定为drawable的id.
但是,您可以在代码中检索属性的主题和特定于平台的drawable:
// 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();
Run Code Online (Sandbox Code Playgroud)
现在,您可以创建一个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);
Run Code Online (Sandbox Code Playgroud)
您还需要设置yourLayoutContainer's clickable属性:
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3110 次 |
| 最近记录: |