[英]set Custom Programatically created drawable to radioButton for states
I wanted to set a programatically created drawable to radio button for its checked and unchecked states, but it is not working my code is as follows,我想为它的选中和未选中状态设置一个以编程方式创建的可绘制到单选按钮,但它不起作用我的代码如下,
Code to draw a rectangular box,绘制矩形框的代码,
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;
}
Code to set programatically created View(squareview) to set as stated to radiobutton,以编程方式创建的视图(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) {
}
}
After a little work around, I realized that the issue is the drawable does not have any size.经过一些工作,我意识到问题是 drawable 没有任何大小。 I'm not sure what size you should give but just adding following line makes your RadioButton
visible:我不确定您应该提供什么尺寸,但只需添加以下行即可使您的RadioButton
可见:
shape.setSize(50, 50);
I would suggest to put appropriate size for it in dimens.xml
and use this instead:我建议在dimens.xml
为其放置适当的大小并使用它:
int size = context.getResources().getDimensionPixelSize(R.dimen.radio_button_size);
shape.setSize(size, size);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.