在Xml里面修改
app:backgroundTint="@color/yourcolor"
在代码里面修改
//获取方法
private ColorStateList getColorStateListTest(int colorRes) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled}, // enabled
new int[]{-android.R.attr.state_enabled}, // disabled
new int[]{-android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_pressed} // pressed
};
int color = ContextCompat.getColor(context, colorRes);
int[] colors = new int[]{color, color, color, color};
return new ColorStateList(states, colors);
}
//调用方法
mfb.setBackgroundTintList(getColorStateListTest(R.color.yourcolor));
如果还想修改前景色,像这样
faoatBtn.setColorFilter(R.color.yourcolor);
本文介绍如何在XML配置文件及代码中修改Android应用的背景颜色,包括使用ColorStateList实现不同状态下的颜色变化,以及如何修改前景色。
2927

被折叠的 条评论
为什么被折叠?



