以编程方式为单选按钮设置自定义可绘制对象 [英] Programmatically set custom drawables for radio buttons

查看:34
本文介绍了以编程方式为单选按钮设置自定义可绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 android 中开发一个基本的绘图应用程序,但我似乎无法以编程方式为我的单选按钮设置自定义可绘制对象.这些单选按钮由一个 LayerDrawable 和一个白色 ColorDrawable 的边框和一个插入的黄色(或任何颜色)ColorDrawable 组成.我将这个 LayerDrawable 与另一个(它有黑色边框表示选择)一起放在 StateListDrawable 中,以保留 RadioButton 功能.

I'm developing a basic paint application in android and I can't seem to programmatically set custom drawables for my radio buttons. These radio buttons consist of a LayerDrawable with a white ColorDrawable for the borders and an inset yellow (or whatever color it is) ColorDrawable for the center. I put this LayerDrawable along with another one (it has black borders to indicate selection) in a StateListDrawable to preserve the RadioButton functionality.

但是当我尝试 setButtonDrawable(myDrawable) 时,即使我指定宽度和高度为 30dpRadioButton 占用的区域非常小>.

But when I try setButtonDrawable(myDrawable), the RadioButton occupies a very small region even though I specify the width and height to be 30dp.

然后当我尝试 setButtonDrawable(null)setBackground(myDrawable) 时,我的 RadioButton 不再具有它的功能.

And then when I try setButtonDrawable(null) and setBackground(myDrawable), my RadioButton no longer has its functionality.

这是我的代码

private void setupColorButtons() {
    int[] colors = {Color.RED, Color.GREEN, Color.BLUE,
                    Color.YELLOW, Color.CYAN, Color.MAGENTA};
    int childCount = mColorGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        RadioButton colorButton = (RadioButton) mColorGroup.getChildAt(i);
        colorButton.setButtonDrawable(createColorButtonDrawable(colors[i]));
    }
}

private Drawable createColorButtonDrawable(int color) {
    ColorDrawable center = new ColorDrawable(color);
    ColorDrawable selBorder = new ColorDrawable(R.color.black);
    ColorDrawable unselBorder = new ColorDrawable(R.color.white);

    LayerDrawable sel = new LayerDrawable(new Drawable[] {selBorder, center});
    sel.setLayerInset(1, 2, 2, 2, 2);
    LayerDrawable unsel = new LayerDrawable(new Drawable[] {unselBorder, center});
    unsel.setLayerInset(1, 2, 2, 2, 2);

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {android.R.attr.state_checked}, sel);
    states.addState(new int[] {-android.R.attr.state_checked}, unsel); 
    return states;
}

我已经看过类似的问题,但每个解决方案都需要我创建自定义 xml 样式.我只是想知道是否有人知道如何以编程方式设置自定义 StateListDrawable .提前感谢您的帮助!

I've already looked at similar questions, but every solution requires me to create custom xml styles. I'm just wondering if anyone knows how to set a custom StateListDrawable programmatically. Thanks in advance for any help!

推荐答案

试试这个代码:

StateListDrawable mState1 = new StateListDrawable();
mState1.addState(new int[] { android.R.attr.state_pressed },
    getResources().getDrawable(R.drawable.button3_pressed));
mState1.addState(new int[] { android.R.attr.state_focused },
    getResources().getDrawable(R.drawable.button3_focused));
mState1.addState(new int[] {},
    getResources().getDrawable(R.drawable.button3_up));
myButton.setBackgroundDrawable(mState1);

这篇关于以编程方式为单选按钮设置自定义可绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆