在新的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"
    />

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


当前回答

芬兰湾的科特林,

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

其他回答

使用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));

你可以使用DrawableCompat。

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

你可以这样使用:

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

对于ImageButton,您可以使用:

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

芬兰湾的科特林,

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