java XML 中是否有等效于 setColorFilter() 的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13137612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:38:10  来源:igfitidea点击:

Is there an equivalent to setColorFilter() in XML?

javaandroidxml

提问by Jecimi

I'd like to know if a ColorFiltercan be set in the XML of a Viewor if the only way is to use setColorFilter()in Java.

我想知道是否ColorFilter可以在 a 的 XML 中设置 aView或者是否唯一的方法是setColorFilter()在 Java 中使用。

采纳答案by Martin Christmann

According to the documentation http://developer.android.com/reference/android/graphics/ColorFilter.htmlthere is no corresponding xml version for ColorFilter.

根据文档 http://developer.android.com/reference/android/graphics/ColorFilter.html,ColorFilter没有相应的 xml 版本。

I also searched for attributes in documentation with the keys 'color' and 'filter' http://developer.android.com/reference/android/R.attr.html

我还使用键“颜色”和“过滤器”在文档中搜索属性 http://developer.android.com/reference/android/R.attr.html

回答by GDanger

There is; it's called tint. It's not available on Viewbut is on ImageViewand ImageButton.

有; 它被称为tint。它在 上不可用,View但在ImageView和上可用ImageButton

If you want to use it with a selectorthis answerhelped me a bunch.

如果你想使用它,selector这个答案对我有很大帮助。

回答by Yogesh Rathi

You can use:

您可以使用:

android:tint="@color/white"

回答by Felipe

There is no xml equivalent, but on API level 21 you can use the tint attribute. But I assume you don't want to target 21+ devices. Not in a few years at least.

没有 xml 等效项,但在 API 级别 21 上,您可以使用 tint 属性。但我假设您不想针对 21 台以上的设备。至少几年内不会。

回答by bentzy

If you want to use tint and support older versions you can Add com.android.support:appcompat-v7:23.2.1to build.gradle (Don't forget to update Android SDK)

如果你想使用 tint 并支持旧版本,你可以添加com.android.support:appcompat-v7:23.2.1到 build.gradle (不要忘记更新 Android SDK)

Thanks for the tip @sherpya!

感谢@shrpya 的提示!