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

2023-09-07 09:19:55 作者:肾虚道长。

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

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:

中间的青色代表被选中的按钮.

The cyan in the middle represents the selected button.

我该怎么做?

推荐答案

RadioButton 类有一个 buttonDrawable 成员,派生自 复合按钮.

您可以通过多种方式更改此可绘制对象.

There are multiple ways you can change this drawable.

来自 java:

myRadioButton.setButtonDrawable(resourceId or Drawable);

来自 xml:

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

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

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

通过右键单击您的 res 文件夹 -> 新建 -> 添加新的选择器 xmlAndroid xml -> 在上方下拉列表中选择 Drawable -> 选择选择器(在 Eclipse 中)要设置此选择器,请查看此 链接从现在开始,您可以将新创建​​的选择器设置为您的任何复选框/单选按钮,使用其 id.