ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何使用appcompat 23.3.0为Button的drawable着色?

2019-05-19 13:13:55  阅读:358  来源: 互联网

标签:android android-drawable appcompat appcompat-v7-r23


我正面临着新的AppCompat 23.3.x和drawables的几个问题.首先,我不得不回去删除:

vectorDrawables.useSupportLibrary = true

因为AppCompat现在还原了,我的应用程序再次生成PNG.好吧,但是,我以一种完全停止工作的方式(对于M之前的设备)给一个按钮(drawableTop)着色.

这是我使用的方法:

private void toggleState(boolean checked) {
    Drawable[] drawables = getCompoundDrawables();
    Drawable wrapDrawable = DrawableCompat.wrap(drawables[1]);
    if (checked) {
        DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),
                R.color.colorPrimary));
        setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
    } else {
        DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),
                R.color.icon_light_enabled));
        setTextColor(ContextCompat.getColor(getContext(), R.color.text_primary_light));
    }
}

问题是,我有一个可检查的自定义Button类,如果选中,drawableTop和text有不同的颜色,如果没有选中它.

这样做是有效的(使用appcompat 23.2.0),但现在,它不是(在Android M下面).信不信由你,但这样做,当它点击setTint时,图标就不再可见了.

为了使它工作,我必须做如下:

 DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),R.color.colorPrimary));
 setCompoundDrawablesWithIntrinsicBounds(null, wrapDrawable, null, null);

所以每当我给它们着色时,我都要再次调用setCompoundDrawablesWithIntrinsicBounds.这样做可以让一切恢复正常.但是,我有点担心每次设置drawable的性能.基本上,我想知道是否有更好的方法或我缺少的东西.

我知道一个Button有setCompoundDrawableTintList,这将是奇妙的,但它的最小API是23级.

解决方法:

没有足够的声誉来发表评论,但我也遇到了这个问题.我今天抓住它而不是在发布之后真的很糟糕和好事.

/**
 * Tinting drawables on Kitkat with DrawableCompat
 * requires wrapping the drawable with {@link DrawableCompat#wrap(Drawable)} prior to tinting it
 * @param view The view containing the drawable to tint
 * @param tintColor The color to tint it.
 */
public static void tintViewBackground(View view, int tintColor) {
    Drawable wrapDrawable = DrawableCompat.wrap(view.getBackground());
    DrawableCompat.setTint(wrapDrawable, tintColor);
    view.setBackground(wrapDrawable);
}

/**
 * Tinting drawables on Kitkat with DrawableCompat
 * requires wrapping the drawable with {@link DrawableCompat#wrap(Drawable)} prior to tinting it
 * @param drawable The drawable to tint
 * @param tintColor The color to tint it.
 */
public static void tintDrawable(Drawable drawable, int tintColor) {
    Drawable wrapDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(wrapDrawable, tintColor);
}

甚至更奇怪的是这只发生在Lollipop上,在Kitkat和API 23上可以看到正常的行为.

标签:android,android-drawable,appcompat,appcompat-v7-r23
来源: https://codeday.me/bug/20190519/1135283.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有