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

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

问题描述

我正在开发Android中一个基本的绘图应用程序,我似乎无法以编程方式设置自定义绘制资源为我的单选按钮。这些单选按钮组成的 LayerDrawable 用白色 Col​​orDrawable 的边界和插图黄色(或它是什么颜色) Col​​orDrawable 为中心。我把这个 LayerDrawable 连同另一个(它有黑色的边框表示选中)在 StateListDrawable 来preserve的单选功能。

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)单选占据即使我指定一个非常小的区域宽度和高度为 30dp

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(空)的setBackground(myDrawable),我的单选不再有它的功能。

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

下面是我的code

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!

推荐答案

试试这个code:

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天全站免登陆