【问题标题】:Android dynamic RadioGroup/RadioButtons as flat buttonsAndroid 动态 RadioGroup/RadioButtons 作为平面按钮
【发布时间】:2011-08-14 20:48:40
【问题描述】:

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

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

这是我的示例,即使我将可绘制按钮设置为 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 |
-------------------

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

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

【问题讨论】:

  • 实际上将可绘制按钮设置为android的空资源有效......我所要做的就是:myRadioButton.setButtonDrawable(android.R.id.empty); myRadioButton.setPadding(5,0,5,0);然后它工作了(在 Galaxy Tab 上)......但是:由于某种原因,当我在 Xoom 上运行它时它崩溃并给我一个“android.content.res.Resources$NotFoundException:来自可绘制资源 ID #___ 的文件”为什么 Xoom 无法访问 android.R.id.empty 资源,而 Galaxy Tab 可以?
  • 修复了这个问题。请参阅此处的帖子:stackoverflow.com/questions/5997359/…

标签: android dynamic radio-button radio-group


【解决方案1】:

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

【讨论】:

  • 我还没有机会尝试这个,但这不会让整个单选按钮消失吗?我只希望单选按钮内的“点”消失。不过谢谢你的建议。 :)
  • 啊,是的,这是真的。错过了一个:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 2011-07-03
  • 2017-11-05
  • 2017-01-14
  • 2012-06-10
相关资源
最近更新 更多