2010-08-26 106 views
84

我想让我的android应用程序的用户能够设置一些参数。单选按钮非常适合这种情况。但是,我不喜欢单选按钮被渲染。是否有可能更改单选按钮组中的单选按钮图标

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

回答

156

有可能的话,你必须定义自己的风格单选按钮,在RES /价值/ 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:

<?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> 

然后只需将自定义主题应用到整个应用程序或您选择的活动。

欲了解更多有关主题和款式的信息,请看http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/这是很好的指导。

+12

您也可以只设置了android:单选按钮本身的按钮属性,而不是定义为它单独的自定义样式。 – 2010-08-26 15:49:21

+6

的确如此,但这不是太好的做法,因为你通常只有一个单选按钮。 – 2010-08-26 15:53:25

+0

@KonstantinBurov如果我使用9补丁图像怎么办? – Hades 2013-02-20 06:37:36

28

您可以将自定义图像放置为单选按钮,如普通按钮。 为创建绘制文件夹一个XML文件 如

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/sub_screens_aus_hl" 
    android:state_pressed="true"/> 
<item android:drawable="@drawable/sub_screens_aus" 
    android:state_checked="true"/> 
<item android:drawable="@drawable/sub_screens_aus" 
    android:state_focused="true" /> 
<item android:drawable="@drawable/sub_screens_aus_dis" /> 
</selector> 

在这里,你可以使用3对不同的图像进行单选

,并使用此文件单选按钮,如:

android:button="@drawable/aus" 
android:layout_height="120dp" 
android:layout_width="wrap_content" 
0

这里可能是一个快速的方法,

enter image description here

有了上述的两种图标,你应该有一个RadioGroup像这样

enter image description here

  • 变化RadioGroup的取向水平
  • 每个RadioButton的属性,尝试给图标为ButtonCompoundButton,
  • 调整填充和大小,
  • ,并在选中时设置背景属性。
7

更简单的方法,只改变单选按钮简直就是为绘制权

<RadioButton 
    ... 
    android:button="@null" 
    android:checked="false" 
    android:drawableRight="@drawable/radio_button_selector" /> 

设置选择和选择是:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/ic_checkbox_checked" android:state_checked="true" /> 
    <item android:drawable="@drawable/ic_checkbox_unchecked" android:state_checked="false" /></selector> 

这是所有

+0

最简单的解决方案!谢谢! – Mangesh 2017-12-03 19:45:29

1

是.. ..` 从XML

android:button="@drawable/yourdrawable" 

,并从Java

myRadioButton.setButtonDrawable(resourceId or Drawable); 

`