在新的AppCompat库中,我们可以这样为按钮着色:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/follow"
    android:id="@+id/button_follow"
    android:backgroundTint="@color/blue_100"
    />

如何在我的代码中以编程方式设置按钮的色调? 我基本上是试图实现基于一些用户输入的按钮的条件着色。


根据文档,android的相关方法:backgroundTint是setBackgroundTintList(ColorStateList list)

更新

按照此链接了解如何创建颜色状态列表资源。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="#your_color_here" />
</selector>

然后使用

setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));

哪里contextInstance是一个Context的实例


使用AppCompart

btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));

你试过这样的东西吗?

button.setBackgroundTintList(getResources().getColorStateList(R.id.blue_100));

注意getResources()将只在活动中工作。但它也可以在任何上下文中调用。


你可以用

button.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.blue_100)));

但是我建议你使用昨天刚刚发布的支持库drawable tinting:

Drawable drawable = ...;

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

你可以在这篇博客文章中找到更多信息(参见“可绘制着色”部分)。


你可以使用DrawableCompat。

public static Drawable setTint(Drawable drawable, int color) {
    final Drawable newDrawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(newDrawable, color);
    return newDrawable;
}

我也遇到过类似的问题。我希望为一个基于颜色(int)值的视图着色一个复杂的可绘制背景。 我成功地使用了代码:

ColorStateList csl = new ColorStateList(new int[][]{{}}, new int[]{color});
textView.setBackgroundTintList(csl);

其中color是一个int值,表示所需的颜色。 这表示简单的xml ColorStateList:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:color="color here"/>
</selector>

希望这能有所帮助。


似乎视图有自己的色调管理机制,所以最好将色调列表:

ViewCompat.setBackgroundTintList(
    editText, 
    ColorStateList.valueOf(errorColor));

对于ImageButton,您可以使用:

favoriteImageButton.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint

我设法让我的工作的方式是使用CompoundButtonCompat。setButtonTintList(按钮、色彩)。

据我所知,无论android版本如何,这都是可行的。


通过提供一个真实的代码情况来适当地扩展dimsuz的回答,请参阅下面的代码片段:

    Drawable buttonDrawable = button.getBackground();
    buttonDrawable = DrawableCompat.wrap(buttonDrawable);
    //the color is a direct color int and not a color resource
    DrawableCompat.setTint(buttonDrawable, Color.RED);
    button.setBackground(buttonDrawable);

此解决方案适用于使用可绘制对象作为按钮背景的场景。它也适用于前棒棒糖时代的设备。


这很容易在材质设计库的新材质按钮中处理,首先,添加依赖项:

implementation 'com.google.android.material:material:1.1.0-alpha07'

然后在你的XML中,使用这个按钮:

<com.google.android.material.button.MaterialButton
    android:id="@+id/accept"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/i_accept"
    android:textSize="18sp"
    app:backgroundTint="@color/grayBackground_500" />

当你想改变颜色时,这是Kotlin中的代码,它没有被弃用,可以在Android 21之前使用:

accept.backgroundTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, 
R.color.colorPrimary, theme))

建议的答案在这里不能正常工作在android 5.0,如果你的XML基于颜色状态列表引用主题属性。 例如,我有一个这样的xml颜色状态列表:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?colorPrimary" android:state_enabled="true"/>
    <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>

使用这个作为我的backgroundTint from xml在android 5.0和其他任何东西上都很好。然而,如果我试图在代码中这样设置:

(不要这样做)

myButton.setSupportButtonTintList(ContextCompat.getColorStateList(myButton.getContext(), R.color.btn_tint_primary));

It actually doesn't matter if I pass the Activity or the button's context to ContextCompat.getColorStateList() method, neither will give me the proper color state list with respect to the theme the button is within. This is because using theme attributes in color state lists wasn't supported until api 23 and ContextCompat does not do anything special to resolve these. Instead you must use AppCompatResources.getColorStateList() which does its own resource parsing/theme attribute resolution on devices < API 23.

相反,你必须这样做:

myButton.setSupportBackgroundTintList(AppCompatResources.getColorStateList(myButton.getContext(), R.color.btn_tint_primary));

TLDR:使用AppCompatResources,而不是- contextcompat -如果你需要在android的所有API版本中解析主题资源。

有关该主题的更多信息,请参阅本文。


下面是如何在kotlin中做到这一点:

view.background.setTint(ContextCompat.getColor(context, textColor))

如果你正在使用Kotlin和Material Design,你可以像这样改变MaterialButton的颜色:

myButton.background.setTintList(ContextCompat.getColorStateList(context, R.color.myColor))

你可以通过为你的MaterialButton创建一个扩展函数来更好地改进它,以使你的代码更易于阅读,你的编码更方便:

fun MaterialButton.changeColor(color: Int) {
    this.background.setTintList(ContextCompat.getColorStateList(context, color))
}

然后,你可以像这样在任何地方使用你的函数:

myButton.changeColor(R.color.myColor)

除了Shayne3000的答案,你还可以使用颜色资源(不仅仅是int颜色)。芬兰湾的科特林版:

var indicatorViewDrawable = itemHolder.indicatorView.background
indicatorViewDrawable = DrawableCompat.wrap(indicatorViewDrawable)
val color = ResourcesCompat.getColor(context.resources, R.color.AppGreenColor, null) // get your color from resources
DrawableCompat.setTint(indicatorViewDrawable, color)
itemHolder.indicatorView.background = indicatorViewDrawable

使用setBackgroundTintList有三个选项

int myColor = Color.BLACK;

按钮。setBackgroundTintList(new ColorStateList(EMPTY, new int[] {myColor})); button.setBackgroundTintList (ColorStateList.valueOf (myColor)); button.setBackgroundTintList (contextInstance.getResources () .getColorStateList (R.color.my_color));



简单的方法

在Java中

myButton.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.white)));

在Kotlin

myButton.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.white)))

简单的我们也可以用于imageview

    imageView.setColorFilter(ContextCompat.getColor(context,
R.color.COLOR_YOUR_COLOR));

checkbox.ButtonTintList = ColorStateList.ValueOf(Android.Color.White);

使用ButtonTintList代替BackgroundTintList


颜色可以添加到按钮如下:

filterBtn.setBackgroundTintList(ContextCompat.getColorStateList(context,R.color.colorPrimary))

如果你不想关心不同版本的android,你可以使用这段代码,基本上任何视图。为我工作。

val states = arrayOf(intArrayOf(android.R.attr.state_enabled))
                    val colors = intArrayOf(Color.GREEN) // your color here
                    val colorStateList = ColorStateList(states, colors)
                  ViewCompat.setBackgroundTintList(yourButtonHere,colorStateList)

Kotlin版本,祝阅读这篇文章的每个人都有美好的一天;)

顺便说一句。如果你创建了一些可绘制的背景形状,这应该只覆盖色调颜色。


你可以这样使用:

myButton.backgroundTintList = AppCompatResources.getColorStateList(context, R.color.primary_variant)

芬兰湾的科特林,

checkbox.buttonTintList = AppCompatResources.getColorStateList(context, color.colorPrimary)