将自定义编程创建的可绘制对象设置为状态的单选按钮
set Custom Programatically created drawable to radioButton for states
我想将一个以编程方式创建的 drawable 设置为单选按钮的选中和未选中状态,但它不起作用我的代码如下,
绘制矩形框的代码,
public static GradientDrawable squareView(int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
//shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
return shape;
}
用于设置以编程方式创建的视图 (squareview) 以设置为单选按钮的代码,
public static void setChecked_Selector(Context context,RadioButton view) {
try {
Drawable pressed=squareView(ContextCompat.getColor(context,R.color.colorBlue),ContextCompat.getColor(context,R.color.colorRed));//new BadgeDrawable(context,colorPressed);
Drawable normal=squareView(ContextCompat.getColor(context,R.color.colorwhite),ContextCompat.getColor(context,R.color.colorRed));
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_checked,},pressed);
states.addState(new int[]{android.R.attr.state_pressed}, pressed);
states.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, pressed);
states.addState(new int[]{android.R.attr.state_checked, -android.R.attr.state_enabled}, pressed);
states.addState(new int[]{}, normal);
view.setButtonDrawable(states);
} catch (Exception e) {
}
}
经过一些变通,我意识到问题是可绘制对象没有任何大小。我不确定您应该提供多大尺寸,但只需添加以下行即可使您的 RadioButton
可见:
shape.setSize(50, 50);
我建议在 dimens.xml
中为其设置适当的大小,然后改用它:
int size = context.getResources().getDimensionPixelSize(R.dimen.radio_button_size);
shape.setSize(size, size);
我想将一个以编程方式创建的 drawable 设置为单选按钮的选中和未选中状态,但它不起作用我的代码如下,
绘制矩形框的代码,
public static GradientDrawable squareView(int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
//shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
return shape;
}
用于设置以编程方式创建的视图 (squareview) 以设置为单选按钮的代码,
public static void setChecked_Selector(Context context,RadioButton view) {
try {
Drawable pressed=squareView(ContextCompat.getColor(context,R.color.colorBlue),ContextCompat.getColor(context,R.color.colorRed));//new BadgeDrawable(context,colorPressed);
Drawable normal=squareView(ContextCompat.getColor(context,R.color.colorwhite),ContextCompat.getColor(context,R.color.colorRed));
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_checked,},pressed);
states.addState(new int[]{android.R.attr.state_pressed}, pressed);
states.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, pressed);
states.addState(new int[]{android.R.attr.state_checked, -android.R.attr.state_enabled}, pressed);
states.addState(new int[]{}, normal);
view.setButtonDrawable(states);
} catch (Exception e) {
}
}
经过一些变通,我意识到问题是可绘制对象没有任何大小。我不确定您应该提供多大尺寸,但只需添加以下行即可使您的 RadioButton
可见:
shape.setSize(50, 50);
我建议在 dimens.xml
中为其设置适当的大小,然后改用它:
int size = context.getResources().getDimensionPixelSize(R.dimen.radio_button_size);
shape.setSize(size, size);