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

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

问题描述

我正在尝试以编程方式将一组 RadioButtons 添加到 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);
}

并且我需要将可绘制按钮设置为 null(我只想在背景上显示文本).使用 android:button="@null" 在 XML 中手动执行此操作效果很好,但我不想对每个单选按钮进行硬编码.我试过只是做 button.setButtonDrawable(null) 但它没有改变任何东西.

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.

非常感谢任何帮助!

推荐答案

你应该这样做:

button.setBackgroundDrawable(null);

如果你的 drawable 引用了选择器,你可以通过选择器 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:button=“@null"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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