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

发布于 2024-11-04 13:23:24 字数 1579 浏览 0 评论 0原文

这与相关帖子中提到的问题类似,但我认为它足够不同得到它自己的问题。事情是这样的:

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

这是我的示例,即使我将按钮可绘制设置为空,无线电圆圈仍然出现。

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

无线电圆圈消失,但文本不会进入无线电圆圈应在的区域。这里有一些 ascii 艺术来展示它的作用:

setButtonDrawable(null): (O = radio Circle)

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

setButtonDrawable(android.R.id.empty):

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

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

对我的问题有任何帮助将不胜感激。谢谢。

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:

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

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(null): (O = radio circle)

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

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.

Any help on my issue would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-11-11 13:23:24

我看到您已经解决了这个问题,但我只是想知道您是否尝试过使用: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文