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

标签 android

看起来我们可以在 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/

相关文章:

android - 在 iOS 和 Android 上处理推送通知场景

java - 获得多功能服务的最佳方式?

javascript - 用于显示 html 内容的 WebView 与 TextView

android - 错误: cannot find symbol class ofArgb

android - 如何使用 Material 设计来挑选时间?

android - 如何禁用状态栏或通知栏但不禁用android中的标题栏?

java - Android 在 HttpClient 中的 403 后得到响应

android - PrintedPdfDocument 总是导致损坏的 PDF

android - 将新的 apk 上传到 prod - 添加了 "unrequested"本地化

部分显示的Android WebView图像