Android动态RadioGroup/RadioButtons作为平面按钮



这是一个类似的问题,在一个相关的帖子中提到,但我认为它是不同的,足以得到一个单独的问题。是这样的:

通过将单选按钮的button属性设置为null,在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);

这是我的例子,即使我设置按钮drawable为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();

如果我将按钮drawable设置为空,像这样:

myRadioButton.setButtonDrawable(android.R.id.empty);

的单选圆圈消失,但文本没有进入单选圆圈应该在的区域。下面是一些ascii艺术来展示它的功能:

setButtonDrawable(null): (0 = radio circle)

-------------------
| O  One | O  Two |
-------------------

setButtonDrawable (android.R.id.empty):

-------------------
|    One |    Two |
-------------------

我已经尝试设置文本重力等,以使文本进入空白空间,但似乎"无线电圈"仍然存在,但它只是不可见。

对于我的问题,任何帮助都将是非常感激的。谢谢。

我看到你已经解决了这个问题,但我只是想知道你是否曾经尝试使用:setVisibility(View.GONE);

最新更新