【问题标题】:Styling radio buttons on ActionBarCompat dropdown menuActionBarCompat 下拉菜单上的样式单选按钮
【发布时间】:2014-03-25 23:29:22
【问题描述】:

我正在开发一个使用扩展 @style/Theme.AppCompat.Light.DarkActionBar 的主题的应用程序。

在我的一项活动中,有一个操作栏图标显示三个带有单选按钮的选项(这里是菜单 XML 文件的摘录):

<item
    android:icon="@drawable/ic_action_filter_white"
    android:showAsAction="always"
    android:title=""
    preparatest:showAsAction="always">
    <menu>
        <group android:checkableBehavior="single" >
            <item
                android:id="@+id/menu_filter_all"                    
                android:title="@string/history_list_filter_all"
                android:checked="true"/>
            <item
                android:id="@+id/menu_filter_pending"
                android:title="@string/history_list_filter_pending"/>
            <item
                android:id="@+id/menu_filter_finished"
                android:title="@string/history_list_filter_finished"/>
        </group>
    </menu>
</item>

我想给这些单选按钮一个自定义样式,但我不知道在哪里做。在我的 styles.xml 文件中,我将自定义样式定义为

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

并尝试使用所有这四个选项,但无济于事:

&lt;item name="android:radioButtonStyle"&gt;@style/RadioButtonPTGreenTheme&lt;/item&gt;

&lt;item name="android:listChoiceIndicatorSingle"&gt;@style/RadioButtonPTGreenTheme&lt;/item&gt;

&lt;item name="android:radioButtonStyle"&gt;@drawable/ptgreentheme_btn_radio_holo_light&lt;/item&gt;

&lt;item name="android:listChoiceIndicatorSingle"&gt;@drawable/ptgreentheme_btn_radio_holo_light&lt;/item&gt;

有人知道怎么做吗?谢谢!

【问题讨论】:

    标签: android android-actionbar android-actionbar-compat


    【解决方案1】:

    请参阅此blog post 以了解方法。

    长话短说,android:actionBarWidgetTheme 用于设置依赖于 Action Bar 的元素的样式,因此 android:radioButtonStyle 应该放在 actionBarWidgetTheme 的样式中:

    <style name="MyTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarWidgetTheme">@style/MyActionBarWidgetTheme</item>
    </style>
    
    <style name="MyActionBarWidgetTheme" parent="@android:style/Theme.Holo">
        <item name="android:radioButtonStyle">@style/MyRadioButtonStyle</item>
    </style>
    

    【讨论】:

    • 非常感谢丹,这很有效! :) 您介意我编辑您的帖子以添加博客帖子中的实际代码示例吗?
    • 值得一提的是,您可能希望您用于 radioButtonStyle 的样式的父级是“Widget.AppCompat.CompoundButton.RadioButton”,您可以在此处更改“android:button”和“机器人:背景”。
    猜你喜欢
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2012-10-27
    • 2021-11-07
    • 2020-10-18
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多