简体   繁体   English

将自定义以编程方式创建的 drawable 设置为用于状态的单选按钮

[英]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.

相关问题 以编程方式创建的可绘制对象未在imageview中显示 - Programatically created drawable not shown in imageview 使用 Drawable 以编程方式创建自定义 EditText - Programatically creating Custom EditText with Drawable 如何使用将自定义可绘制对象应用于 RadioButton? - How to use apply a custom drawable to RadioButton? Android:将边距设置为可绘制单选按钮的左侧 - Android: Set margin to left side of radiobutton drawable Android:以编程方式设置单选按钮文本的背景色 - Android: set background color of radiobutton text programatically 如何以编程方式设置 RadioButton Android Circle 的颜色 - How to programatically set the colour of a RadioButton Android Circle 如何在自定义布局中设置以编程方式创建的图像的父对齐 - How to Set Parent Alignment of Programatically Created Image's in Custom Layout 如何将可绘制的xml文件添加到使用For循环创建的单选按钮中 - how to add drawable xml file into radiobutton that is created using For loop 有没有办法以编程方式设置android:drawable的背景? - Is there a way to set background from android:drawable programatically? Android Studio:如何以编程方式将Drawable设置为buttonTextColor? - Android Studio : How to set a Drawable to a buttonTextColor Programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM