在 Android 中制作带有图像的 RadioGroup 单选按钮......如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11853245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:01:11  来源:igfitidea点击:

Making RadioGroup radio buttons with images in Android... How?

androidradio-buttonradio-group

提问by Igal

Is it possible to make Radio Buttons in Android, where there are image representation to a button instead of a regular buttons? I want it to look something like that:

是否可以在 Android 中制作单选按钮,其中按钮有图像表示而不是常规按钮?我希望它看起来像这样:

enter image description here

在此处输入图片说明

The cyan in the middle represents the selected button.

中间的青色代表选中的按钮。

How can I do it?

我该怎么做?

回答by Balázs édes

The RadioButtonclass has a buttonDrawablemember, derived from CompoundButton.

RadioButton类有一个buttonDrawable构件,衍生自CompoundButton

There are multiple ways you can change this drawable.

有多种方法可以更改此可绘制对象。

From java:

从爪哇:

myRadioButton.setButtonDrawable(resourceId or Drawable);

From xml:

来自 xml:

<RadioButton
    android:button="@drawable/yourdrawable" 
    ...
/>

If you just want the checked/unchecked state look differently, then

如果您只是希望选中/未选中状态看起来不同,那么

  • Add a new selector xml by rightclicking your res folder -> New -> Android xml -> select Drawable in the upper dropdown -> choose selector (in eclipse)
  • To setup this selector, please take a look at this link
  • From now on you can set your freshly created selector to any of your checkboxes/radiobuttons by using its id.
  • 通过右键单击 res 文件夹添加新的选择器 xml -> 新建 -> Android xml -> 在上方下拉列表中选择 Drawable -> 选择选择器(在 eclipse 中)
  • 要设置此选择器,请查看此链接
  • 从现在开始,您可以使用其 id 将新创建的选择器设置为任何复选框/单选按钮。