首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在RippleDrawable中更改StateListDrawable的TintList

在RippleDrawable中更改StateListDrawable的TintList
EN

Stack Overflow用户
提问于 2015-07-03 02:49:58
回答 2查看 2.8K关注 0票数 3

作用域

RippleDrawable内部有selector作为item。它起作用了。

代码语言:javascript
复制
<?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>

+

代码语言:javascript
复制
<?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更改选择器的默认状态颜色

代码语言:javascript
复制
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最好是完全加载的;

代码语言:javascript
复制
<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>

+

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

发布于 2015-07-03 02:53:19

你试过someView.setBackground(bgWrap)吗?您将tint设置为bgWrap,但您还需要将其设置为背景,而不是您的旧背景。

编辑:你确定setTintList被窃听了吗?下面的代码适用于所有带有AppCompat的API >= 15:

代码语言:javascript
复制
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);
}
票数 2
EN

Stack Overflow用户

发布于 2015-07-03 03:02:41

问题172067: DrawableCompat#setTintList在棒棒糖和更高版本上不起作用

https://code.google.com/p/android/issues/detail?id=172067

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31192375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档