代码之家  ›  专栏  ›  技术社区  ›  yamspog

是否可以更改android单选按钮组中的单选按钮图标

  •  90
  • yamspog  · 技术社区  · 15 年前

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

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

    5 回复  |  直到 15 年前
        1
  •  168
  •   Konstantin Burov    15 年前

    是的,这是可能的,你必须定义你自己的风格单选按钮,在res/值/样式.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>
    

        <?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/ 那是个好向导。

        2
  •  30
  •   Vivek Kalkur Sujit    13 年前

    你可以像普通按钮一样在单选按钮中放置自定义图像。 例如

    <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" 
    
        3
  •  15
  •   topcbl    9 年前

    只更改单选按钮的简单方法是将选择器设置为drawable right

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

    这就是全部

        4
  •  2
  •   shweta jariya    8 年前

    是的`

    android:button="@drawable/yourdrawable" 
    

    来自Java

    myRadioButton.setButtonDrawable(resourceId or Drawable);
    

    `

        5
  •  0
  •   Aung Myo Linn    9 年前

    enter image description here

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

    enter image description here

    • 更改 单选 的水平方向
    • 对于每个 RadioButton Button CompoundButton ,
    • 调整衬垫和尺寸,
        6
  •  0
  •   Abhishek Kumar    5 年前

    如果你想用编程的方式来做,

    checkBoxOrRadioButton.setButtonDrawable(null);
    checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);