是否有可能改变在Android单选按钮组的单选按钮图标单选、有可能、按钮、按钮图标

2023-09-11 11:27:38 作者:绯闻男友

我想,让我的Andr​​oid应用程序的用户设置一些参数的能力。单选按钮是理想的这种情况。但是,我不喜欢按钮呈现收音机。

I am wanting to allow the user of my android application the ability to set some parameters. The radio button is ideal for this situation. However, I don't like the radio buttons are rendered.

是否有可能改变的单选按钮图标?例如,是否有可能创建一个自定义布局的每一行,并在布局中引用自己的图标,更改字体等。

Is it possible to change the radio button icon? For example, is it possible to create a custom layout for each row and in that layout reference my own icon and change the font et al.

推荐答案

有可能的话,你必须定义自己的风格单选按钮,在RES /价值/ styles.xml:

Yes that's possible you have to define your own style for radio buttons, at res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
   <item name="android:radioButtonStyle">@style/RadioButton</item>
</style>
<style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
   <item name="android:button">@drawable/radio</item>
</style>
</resources>

单选这里应该是一个有状态的绘制,radio.xml:

'radio' here should be a stateful drawable, radio.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_window_focused="false"
        android:drawable="@drawable/radio_hover" />
    <item android:state_checked="false" android:state_window_focused="false"
        android:drawable="@drawable/radio_normal" />
    <item android:state_checked="true" android:state_pressed="true"
        android:drawable="@drawable/radio_active" />
    <item android:state_checked="false" android:state_pressed="true"
        android:drawable="@drawable/radio_active" />
    <item android:state_checked="true" android:state_focused="true"
        android:drawable="@drawable/radio_hover" />
    <item android:state_checked="false" android:state_focused="true"
        android:drawable="@drawable/radio_normal_off" />
    <item android:state_checked="false" android:drawable="@drawable/radio_normal" />
    <item android:state_checked="true" android:drawable="@drawable/radio_hover" />
    </selector>

然后,只需应用自定义主题,无论是整个应用程序或您所选择的活动。

Then just apply the Custom theme either to whole app or to activities of your choice.

有关主题和样式更多信息看http://brainflush.word$p$pss.com/2009/03/15/understanding-android-themes-and-styles/这是很好的指导。

For more info about themes and styles look at http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/ that is good guide.