作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
RippleDrawable 内部有 selector
作为 item
。它有效。
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/pink_highlight">
<item
android:id="@android:id/mask"
android:drawable="@color/pink_highlight" />
<item
android:drawable="@drawable/bg_selectable_item" />
</ripple>
+
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pink_highlight_focus" android:state_focused="true" />
<item android:drawable="@color/pink_highlight_press" android:state_pressed="true" />
<item android:drawable="@color/pink_highlight_press" android:state_activated="true" />
<item android:drawable="@color/pink_highlight_press" android:state_checked="true" />
<item android:drawable="@android:color/transparent" />
</selector>
无法使用 DrawableCompat.setTintList
更改选择器的默认状态颜色
RippleDrawable bg = (RippleDrawable)
ResourcesCompat.getDrawable(context.getResources(), R.drawable.bg_navigation_item, null);
StateListDrawable bgWrap = (StateListDrawable) DrawableCompat.wrap(bg.getDrawable(1));
DrawableCompat.setTintList(bgWrap, new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.WHITE}));
//
someView.setBackground(bg);
它不会改变默认选择器的状态,其他一切正常。
问题发生是因为- 对着色应该如何工作的误解;- ColorStateList
最好完全加载;
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/pink_highlight">
<item>
<shape
android:shape="rectangle"
android:tint="@color/selectable_transparent_item" />
</item>
<item
android:id="@android:id/mask"
android:drawable="@color/pink_highlight" />
</ripple>
+
LayerDrawable bg = (LayerDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.bg_selectable_item, null);
Drawable bgWrap = DrawableCompat.wrap(bg.getDrawable(0));
DrawableCompat.setTintList(bgWrap, context.getResources().getColorStateList(R.color.selectable_white_item));
someView.setBackground(bg);
最佳答案
你试过 someView.setBackground(bgWrap)
了吗?您将色调设置为 bgWrap
,但您还需要将其设置为背景,而不是您的旧背景。
编辑:您确定 setTintList
被窃听了吗?以下代码适用于所有 API >= 15 和 AppCompat:
public static void tintWidget(View view, ColorStateList colorStateList) {
final Drawable originalDrawable = view.getBackground();
final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
DrawableCompat.setTintList(wrappedDrawable, colorStateList);
view.setBackground(wrappedDrawable);
}
关于android - 在 RippleDrawable 中为 StateListDrawable 更改 TintList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192375/
决定尝试新的 DrawableCompat 类。按照 reliable source 的说明进行操作, 我打电话: Button b = (Button) findViewById(R.id.butt
范围 RippleDrawable 内部有 selector 作为 item。它有效。 + 问题 无法使
我是一名优秀的程序员,十分优秀!