这与 related post 中提到的问题类似但我认为它有足够的不同来提出自己的问题。开始了:
通过将单选按钮的按钮属性设置为空,在 xml 中声明单选按钮时,我已经能够让“单选圆圈”消失,没有问题:
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One"
android:background="@drawable/radio_button_selector"
android:button="@null"/>
但是当我尝试动态声明单选按钮时,即使我这样做也无法让单选圆圈消失:
myRadioButton.setButtonDrawable(null);
这是我的示例,即使我将可绘制按钮设置为 null,单选圆圈仍然出现。
RadioGroup myRadioGroup = (RadioGroup)findViewById(R.id.myRadioGroup);
for (int i = 0; i < arrayListOfRadioButtonStringNames.size(); i++)
{
RadioButton myRadioButton = new RadioButton(this);
myRadioButton.setText(arrayListOfRadioButtonStringNames.get(i));
myRadioButton.setButtonDrawable(null);
myRadioButton.setBackgroundResource(R.drawable.radio_button_selector);
myRadioGroup.addView(myRadioButton);
}
myRadioGroup.invalidate();
如果我改为将可绘制按钮设置为空,如下所示:
myRadioButton.setButtonDrawable(android.R.id.empty);
radio 圈消失了,但文本没有进入 radio 圈应该在的区域。这里有一些 ascii 艺术来展示它的作用:
setButtonDrawable(null): (O = radio circle)
-------------------
| O One | O Two |
-------------------
setButtonDrawable(android.R.id.empty):
-------------------
| One | Two |
-------------------
我已经尝试设置文本引力等以使文本进入该空白区域,但似乎“ radio 圈”仍然存在,只是不可见。
如能就我的问题提供任何帮助,我们将不胜感激。谢谢。
最佳答案
我看到你已经解决了这个问题,但我只是想知道你是否尝试过使用:setVisibility(View.GONE);
我认为这应该有效。
关于Android 动态 RadioGroup/RadioButtons 作为平面按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821669/