如何以编程方式设置 android:button=“@null"?

How can I programmatically set android:button=quot;@nullquot;?(如何以编程方式设置 android:button=“@null?)
本文介绍了如何以编程方式设置 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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)