看起来我们可以在 RadioButton 中使用以下内容:
android:button="@drawable/myCustomStateBackground"
但是那个drawable只占据了radiodrawable通常会去的地方。理想情况下,我希望我的整个按钮背景都是有状态的。所以当按下时,我希望整个按钮看起来像卡在按下状态。为此,我希望我可以这样做:
android:button="null"
android:background="@drawable/myCustomStateBackground"
但是背景drawable不知道推送状态,就像按钮属性一样。有办法解决吗?
最佳答案
给你的单选按钮一个自定义样式:
<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>
用你自己的替换drawables。
关于android - 如何使用将自定义可绘制对象应用于 RadioButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12432553/