如何使用将自定义可绘制对象应用于RadioButton? [英] How to use apply a custom drawable to RadioButton?
问题描述
看起来我们可以在RadioButton中使用以下内容:
It looks like we can use the following in a 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:
android:button="null"
android:background="@drawable/myCustomStateBackground"
,但是背景可绘制对象不知道推送状态,就像button属性一样.有办法解决吗?
but then the background drawable doesn't know about push state, like the button attribute does. Is there a way around it?
推荐答案
为单选按钮提供自定义样式:
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
<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>
用您自己的绘画代替.
这篇关于如何使用将自定义可绘制对象应用于RadioButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!