单选按钮 - 如何使用自定义绘制? [英] RadioButton - how to use a custom drawable?
问题描述
看起来我们可以使用一个单选按钮以下内容:
It looks like we can use the following in a RadioButton:
android:button="@drawable/myCustomStateBackground"
但绘制只占那个地方无线电绘制通常会去。理想我想我的整个按钮的背景是有状态的。所以,当推,我想整个按钮看起来像它停留在pressed状态。为了做到这一点,我希望我可以做这样的事情:
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"
但那么背景抽拉不知道关于推状态,像按钮属性一样。有没有变通的办法?
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>
替换可绘有你自己的。
Replace the drawables with your own.
这篇关于单选按钮 - 如何使用自定义绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!