Android/Java: 如何用int Color设置MaterialShapeDrawable的背景颜色?
Android/Java: how to set background color of MaterialShapeDrawable with int Color?
我有一个带圆角的 TextView。
这是我的代码:
float radius = 10f;
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(textView,shapeDrawable);
现在,我想以编程方式更改 textView 的背景颜色。
当我这样做时:
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.design_default_color_background));
有效;背景颜色已更改。
现在,我想用 Color.RED 之类的 int 颜色或用 Color.RGB(r, g, b, a) 或 Color.RGB 定义的任何随机颜色更改背景颜色(r, g, b).
我该怎么做?我应该使用 shapeDrawable.setFillColor 还是其他方法?
谢谢。
您可以像这样为您的颜色定义全局值
public static final int RED = 0xffff0000;
然后像这样使用。
shapeDrawable.setFillColor(ContextCompat....(this, RED));
方法 setFillColor
适用于 ColorStateList
。
您可以使用类似的东西:
int[][] states = new int[][] {
new int[] { android.R.attr.state_focused}, // focused
new int[] { android.R.attr.state_hovered}, // hovered
new int[] { android.R.attr.state_enabled}, // enabled
new int[] { } //
};
int[] colors = new int[] {
Color.BLACK,
Color.RED,
Color....,
Color....
};
ColorStateList myColorList = new ColorStateList(states, colors);
回答我的问题。
这是我添加的代码,以便能够设置任何背景颜色:
int[][] states = new int[][] {
new int[] { android.R.attr.state_hovered}, // hovered
};
int[] colors = new int[] {color};
ColorStateList myColorList = new ColorStateList(states, colors);
shapeDrawable.setFillColor(myColorList);
shapeDrawable.setState(states[0]);
为了改变背景颜色而不得不写这么多代码真是太疯狂了...
感谢大家的帮助!
我有一个带圆角的 TextView。
这是我的代码:
float radius = 10f;
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(textView,shapeDrawable);
现在,我想以编程方式更改 textView 的背景颜色。
当我这样做时:
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.design_default_color_background));
有效;背景颜色已更改。
现在,我想用 Color.RED 之类的 int 颜色或用 Color.RGB(r, g, b, a) 或 Color.RGB 定义的任何随机颜色更改背景颜色(r, g, b).
我该怎么做?我应该使用 shapeDrawable.setFillColor 还是其他方法?
谢谢。
您可以像这样为您的颜色定义全局值
public static final int RED = 0xffff0000;
然后像这样使用。
shapeDrawable.setFillColor(ContextCompat....(this, RED));
方法 setFillColor
适用于 ColorStateList
。
您可以使用类似的东西:
int[][] states = new int[][] {
new int[] { android.R.attr.state_focused}, // focused
new int[] { android.R.attr.state_hovered}, // hovered
new int[] { android.R.attr.state_enabled}, // enabled
new int[] { } //
};
int[] colors = new int[] {
Color.BLACK,
Color.RED,
Color....,
Color....
};
ColorStateList myColorList = new ColorStateList(states, colors);
回答我的问题。
这是我添加的代码,以便能够设置任何背景颜色:
int[][] states = new int[][] {
new int[] { android.R.attr.state_hovered}, // hovered
};
int[] colors = new int[] {color};
ColorStateList myColorList = new ColorStateList(states, colors);
shapeDrawable.setFillColor(myColorList);
shapeDrawable.setState(states[0]);
为了改变背景颜色而不得不写这么多代码真是太疯狂了...
感谢大家的帮助!