【问题标题】:Android. ImageView as RadioButton [duplicate]安卓。 ImageView 作为 RadioButton [重复]
【发布时间】:2016-08-22 09:57:14
【问题描述】:
我想要一些ImageViews 充当RadioButtons。但我不希望RadioButton 的小按钮选择它;我希望这个迷你按钮消失,并且 ImageView 成为整个按钮。因此,像下面这样的解决方案并不好。
<RadioButton
android:id="@+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/myIcon"/>
是否有一个小部件或其他东西可以用来将ImageViews 用作RadioButtons,或者我应该自己编写按钮的所有逻辑?
【问题讨论】:
标签:
android
imageview
android-imageview
android-radiogroup
android-radiobutton
【解决方案1】:
我会建议创建您的自定义单选按钮。查看 Evan Bashir
的
answer
【解决方案2】:
您可以尝试使用 button 属性,如下所示。
<RadioButton
android:id="@+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/myIcon"/>
或通过语法。
myRadioButton.setButtonDrawable(resourceId or Drawable);
您可以通过以下xml文件在选择RB的同时更改图像...将此文件保存在drawable文件夹中并在button属性或语法中调用它。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/star_down" />
<item android:state_checked="false" android:drawable="@drawable/star" />
</selector>
【解决方案3】:
只需在 RadioButton 的 XML 声明的android:background 中设置图像。
还将android:button 设置为透明:android:button="@android:color/transparent"
如果您想拥有不同的状态 - 为两种状态创建一个包含不同图像的选择器。