棒棒糖设备上的Android setBackgroundTintList [英] Android setBackgroundTintList on pre-lollipop devices

查看:1172
本文介绍了棒棒糖设备上的Android setBackgroundTintList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FloatingActionButton. 用户应该能够在onClick事件中切换FAB背景颜色.

I'm working with FloatingActionButton. The user should be able to switch the FAB background color within a onClick Event.

但是,推荐的对setBackgroundTintList的调用似乎仅与21+ API兼容.

However, the recommended call to setBackgroundTintList seems to be only compatible from 21+ API.

我如何正确地在棒棒糖之前的设备上进行操作?我可以使用其他替代方法吗?

How do I - correctly - go about it on pre-lollipop devices? Is there any alternative I could use?

谢谢.

推荐答案

您也可以使用

You can use also setSupportBackgroundTintList

对背景可绘制对象应用色彩.不修改当前的色调模式,默认为SRC_IN.

Applies a tint to the background drawable. Does not modify the current tint mode, which is SRC_IN by default.

随后对View.setBackground(Drawable)的调用将自动更改可绘制对象并应用指定的着色和着色模式.

Subsequent calls to View.setBackground(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode.

还可以查看

Also take a look on ViewCompat.setBackgroundTintList()

对背景可绘制对象应用色彩.

Applies a tint to the background drawable.

在API v21或更高版本上运行时,这将始终生效.在API v21之前的平台上运行时,只有在视图实现TintableBackgroundView接口后,它才会生效.

This will always take effect when running on API v21 or newer. When running on platforms previous to API v21, it will only take effect if view implement the TintableBackgroundView interface.

我在SO上找到了一个解决方案在此处,这是这样的:

I found a solution here on SO that I've used before and is this:

public static void setButtonTint(Button button, ColorStateList tint) {
  if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
      ((AppCompatButton) button).setSupportBackgroundTintList(tint);
  } else {
      ViewCompat.setBackgroundTintList(button, tint);
  }
}

它对我有用,我希望它对您也有用.

It works for me I hope it works for you too.

这篇关于棒棒糖设备上的Android setBackgroundTintList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆