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

Android dynamic RadioGroup/RadioButtons as flat buttons(Android 动态 RadioGroup/RadioButtons 作为平面按钮)

本文介绍了Android 动态 RadioGroup/RadioButtons 作为平面按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与 related post 中提到的问题类似,但我认为是不同的足以得到它自己的问题.就是这样:

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:

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

这是我的示例,即使我将可绘制按钮设置为 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(null): (O = radio circle)

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.

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

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.

这篇关于Android 动态 RadioGroup/RadioButtons 作为平面按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android 动态 RadioGroup/RadioButtons 作为平面按钮

基础教程推荐