Fer*_*gre 5 android android-appcompat android-drawable
我正面临着新的AppCompat 23.3.x和drawables的几个问题.首先,我不得不回去删除:
vectorDrawables.useSupportLibrary = true
Run Code Online (Sandbox Code Playgroud)
因为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));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,我有一个可检查的自定义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);
Run Code Online (Sandbox Code Playgroud)
所以每当我给它们着色时,我都要再打电话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);
}
Run Code Online (Sandbox Code Playgroud)
甚至更奇怪的是这只发生在Lollipop上,在Kitkat和API 23+上可以看到正常的行为.
| 归档时间: |
|
| 查看次数: |
2205 次 |
| 最近记录: |