Android:如何从资源中获取ColorStateList?

时间:2015-09-01 15:23:43

标签: android

我正在制作导航抽屉,其中图标根据文本的颜色着色。

这是我在res / drawable

中声明的选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/emerald"/>
    <item android:state_selected="true" android:color="@color/emerald"/>
    <item android:state_pressed="true" android:color="@color/emerald"/>
    <item android:color="@android:color/white"/>
</selector>

这是我的ViewHolder

        Drawable drawable = ContextCompat.getDrawable(mContext,iconResourceId);
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable.mutate(),mContext.gerResouces.getColorStateList());
         mItemIcon.setImageDrawable(drawable);

正如你可以看到我遇到的问题是在这一行,我在getColorStateList中传递了什么? doucmentation没有帮助我。

DrawableCompat.setTintList(drawable.mutate(),mContext.gerResouces.getColorStateList());

2 个答案:

答案 0 :(得分:8)

<script type="text/javascript">  

    function replaceSrc()
    {

        var images = document.getElementsByTagName('img');

        for(var i = 0; i < images.length; i++)
        {
            var dt = new Date();
            var img = images[i];

            if(img.src.length >= 0 & img.id != 'idImageNoTimestamp')
            {
                img.src = img.src + "?" + dt.getTime();
                //javascript:alert(document.lastModified);
            }
        }
    }
    replaceSrc();
      </script>

答案 1 :(得分:7)

传递颜色状态列表资源的id,例如R.color.my_color_state_list。颜色状态列表属于res/color,而不是res/drawable

DrawableCompat.setTintList(drawable.mutate(),
    mContext.getResources().getColorStateList(R.color.my_color_state_list));