Android RadioButton - 如何使用自定义可绘制对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12432553/
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 10:38:49  来源:igfitidea点击:

RadioButton - how to use a custom drawable?

android

提问by user291701

It looks like we can use the following in a RadioButton:

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

android:button="@drawable/myCustomStateBackground"

but that drawable only occupies the spot where the radio drawable would normally go. Ideally I want my entire button background to be stateful. So when pushed, I want the entire button to look like its stuck in the pressed state. To do that, I was hoping I could do something like:

但是那个 drawable 只占据了 radio drawable 通常会去的地方。理想情况下,我希望我的整个按钮背景都是有状态的。因此,当按下时,我希望整个按钮看起来像是卡在按下状态。要做到这一点,我希望我可以做这样的事情:

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

but then the background drawable doesn't know about push state, like the button attribute does. Is there a way around it?

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

回答by Benito Bertoli

Give your radiobutton a custom style:

给你的单选按钮一个自定义样式:

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

custom_btn_radio.xml

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>

Replace the drawables with your own.

用您自己的可绘制对象替换。

回答by Tore Rudberg

You should set android:button="@null"instead of "null".

您应该设置android:button="@null"而不是"null".

You were soo close!

你太接近了!

回答by Ali Imran

full solution here:

完整解决方案在这里:

<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>

selector XML

选择器 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>

回答by John smith

if you want to change the only icon of radio button then you can only add android:button="@drawable/ic_launcher"to your radio button and for making sensitive on click then you have to use the selector

如果您想更改单选按钮的唯一图标,那么您只能添加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>

and set to your radio android:background="@drawable/name_of_selector"

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

回答by Gayathri

In programmatically, add the background image

以编程方式,添加背景图像

minSdkVersion 16

minSdk 版本 16

RadioGroup rg = new RadioGroup(this);
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(R.drawable.account_background);
rg.addView(radioButton);