如何使用将自定义可绘制对象应用于RadioButton?


78

看起来我们可以在RadioButton中使用以下内容:

android:button="@drawable/myCustomStateBackground"

但是该可绘制对象仅占据无线电可绘制对象正常运行的位置。理想情况下,我希望我的整个按钮背景都保持有状态。因此,当我按下按钮时,我希望整个按钮看起来像卡在按下状态。为此,我希望可以做些类似的事情:

android:button="null"
android:background="@drawable/myCustomStateBackground"

但是背景可绘制对象不知道按钮状态,就像button属性一样。有办法解决吗?


您可能不是在寻找背景,而是在寻找android:button属性。
MH。

Answers:


113

给您的单选按钮自定义样式:

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:button">@drawable/custom_btn_radio</item>
</style>

custom_btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/btn_radio_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/btn_radio_off" />

    <item android:state_checked="true" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_radio_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_radio_off_selected" />

    <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

用您自己的绘画代替。


5
抱歉-这只会修改“按钮”可绘制对象-我想修改背景可绘制对象-还要使其状态像“按钮”属性一样-看起来RadioButton不支持吗?
user291701

1
使用这种绘制一次我的单选按钮被选中它从来没有得到选中
umerk44

@ umerk44如果RadioButton不属于组,这就是该方法的工作方式。如果需要使它不可选择,则可以使用CheckBox。
伊万·伍尔

37

完整的解决方案在这里:

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/oragne_toggle_btn"
        android:checked="true"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/oragne_toggle_btn"
        android:layout_marginTop="20dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/oragne_toggle_btn"
        android:layout_marginTop="20dp"
        android:text="RadioButton" />
</RadioGroup>

选择器XML

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/orange_btn_selected" android:state_checked="true"/>
<item android:drawable="@drawable/orange_btn_unselected"    android:state_checked="false"/>

 </selector>

2
或者您可以使用android:button="@drawable/oragne_toggle_btn"
pepela

1
不要使用背景属性,请使用> android:button =“ @ drawable / oragne_toggle_btn”
Biswatma

文字颜色呢?
Pratik Butani


8

如果要更改单选按钮的唯一图标,则只能添加android:button="@drawable/ic_launcher"到单选按钮,并且要使单击敏感,则必须使用选择器

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/image_what_you_want_on_select_state" android:state_checked="true"/>
<item android:drawable="@drawable/image_what_you_want_on_un_select_state"    android:state_checked="false"/>


 </selector>

并设置到您的收音机 android:background="@drawable/name_of_selector"


如果m使用android:button =“ @ null” android:background =“ @ drawable / myCustomStateBackground”作为位于导航抽屉菜单中的单选按钮。它无法正常工作。有时可以看到背景图像,但有时看不到。请帮助有关。
威士忌

0

以编程方式添加背景图片

minSdkVersion 16

RadioGroup rg = new RadioGroup(this);
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(R.drawable.account_background);
rg.addView(radioButton);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.