2017-04-10 81 views
1

我想更改View的Backgroundtint,这是我找到的唯一方法。但是我收到一条消息,说它只适用于某些API。我能以某种方式使用API​​ 15吗?或者实际上有更好的方法来显示用户一个按钮被选中?API 21之前的View.setBackgroundTintList?

回答

2

ViewCompat.setBackgroundTintList()会做的伎俩高达API 4.

适用色调的背景绘制。

在API v21或更新版本上运行时,这将始终生效。在API v21之前的平台上运行时,只有在视图实现TintableBackgroundView界面时才会生效。

+0

感谢它的工作原理,即使点击有这个非常丑陋的动画。 – newToEverything

1

是,

使用Java代码可以更改任何视图的绘制颜色这样像:

public static void setIconColor(TextView tv, int which, int color) { 
     tv.getCompoundDrawables()[which].mutate(); 
     tv.getCompoundDrawables()[which].setColorFilter(ContextCompat.getColor(tv.getContext(), color), PorterDuff.Mode.SRC_ATOP); 
    } 

    public static void setIconColor(ImageView imageView, int color) { 
     imageView.getDrawable().mutate(); 
     imageView.getDrawable().setColorFilter(ContextCompat.getColor(imageView.getContext(), color), PorterDuff.Mode.SRC_ATOP); 
    } 

对于用户按钮来选择?你应该使用

android:background="?attr/selectableItemBackground" 

希望它能帮助你!