Android的动态RadioGroup中/单选按钮为平面按钮按钮、单选、平面、动态

2023-09-08 08:51:10 作者:爱上孤独

这是一个类似的问题是什么提到了相关帖子但我认为它足够不同来获得它自己的问题。这里有云:

this is a similar issue to what is mentioned in a related post but I thought it was different enough to get its own question. Here it goes:

我已经能够得到无线电圈通过设置单选按钮的按钮属性声明XML中的单选按钮时,空这样的消失没有问题:

I have been able to get the "radio circle" to disappear no problem when declaring the radio buttons in xml by setting the button attribute of the radio button to null like this:

<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"/>

但是,当我尝试和动态申报单选按钮,我不能让我这样做电台圈甚至消失:

But when i try and declare the radio buttons dynamically i cannot get the radio circle to disappear even when I do this:

myRadioButton.setButtonDrawable(null);

下面是我的榜样,即使我设置按钮绘制为空无线电圈仍然出现。

Here is my example and even though i set the button drawable to null the radio circle still appears.

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();

如果我,而不是设置按钮可绘制空,像这样的:

if I instead set the button drawable to empty, like this:

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

无线电圈消失,但文字不进入其中无线电圈子应该是区。下面是一些ASCII艺术展现它做什么:

the radio circle disappears, but the text doesn't go into the area where the radio circle should be. Here's some ascii art to show what it does:

setButtonDrawable(空):(O =无线圈)

setButtonDrawable(null): (O = radio circle)

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

setButtonDrawable(android.R.id.empty):

setButtonDrawable(android.R.id.empty):

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

我已经尝试设置文本重力等来获取文本要在空的空间,但似乎单选圈仍然存在,但它只是不可见。

I have tried setting the text gravity, etc to get the text to go in that empty space, but it seems that the "radio circle" is still there but its just not visible.

这是我的问题的任何帮助将是AP preciated。谢谢你。

Any help on my issue would be appreciated. Thanks.

推荐答案

我看到你已经解决了这个问题,但我只是想知道,如果你曾经尝试过使用: setVisibility(View.GONE) ; 我认为这应该工作。

I saw that you already fixed this issue, but I'm just wondering if you ever tried using: setVisibility(View.GONE); I think that should work.