如何以编程方式设置的android:按钮=" @空"? [英] How can I programmatically set android:button="@null"?

查看:116
本文介绍了如何以编程方式设置的android:按钮=" @空"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式添加一组单选按钮到一个RadioGroup中,像这样:

I'm trying to programmatically add a set of RadioButtons to a RadioGroup like so:

for (int i = 0; i < RANGE; i++) {
    RadioButton button = new RadioButton(this);
    button.setId(i);
    button.setText(Integer.toString(i));
    button.setChecked(i == currentHours); // Select button with same index as currently selected number of hours
    button.setButtonDrawable(Color.TRANSPARENT);
    button.setBackgroundResource(R.drawable.selector);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            ((RadioGroup)view.getParent()).check(view.getId());
            currentHours = view.getId();
        }
    });

    radioGroupChild.addView(button);
}

和我需要设置按钮绘制为空(我想对我的背景顶部只是文本)。手动操作与的XML安卓按钮=@空的伟大工程,但我不想硬code每个单选按钮。我试着只是做 button.setButtonDrawable(空),但它不会改变任何东西。

and I need to set the button drawable to null (I want just text on top of my background). Manually doing this in the XML with android:button="@null" works great, but I don't want to hardcode each radio button. I've tried just doing button.setButtonDrawable(null) but it doesn't change anything.

任何帮助是极大AP preciated!

Any help is greatly appreciated!

推荐答案

您应该这样做:

button.setBackgroundDrawable(null);

如果您绘制的引用,选择器你可以把它透明通过您选择的xml:

If your drawable has a reference to selector you can make it transparent through your selector xml:

<item android:drawable="@android:color/transparent" />

因为你可能已经找到了解决方案;)

as you'd probably found the solution ;)

这篇关于如何以编程方式设置的android:按钮=&QUOT; @空&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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