Adding custom radio buttons in android

前端 未结 10 771
慢半拍i
慢半拍i 2020-11-22 11:43

I am trying to get radiobutton effect for regular buttons in android

I have a simple android radio button below

相关标签:
10条回答
  • 2020-11-22 12:32

    Add a background drawable that references to an image, or a selector (like below), and make the button transparent:

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/yourbuttonbackground"
        android:checked="true"
        android:text="RadioButton1" />
    

    If you would like your radio buttons to have a different resource when checked, create a selector background drawable:

    res/drawable/yourbuttonbackground.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:drawable="@drawable/b"
            android:state_checked="true"
            android:state_pressed="true" />
        <item
            android:drawable="@drawable/a"
            android:state_pressed="true" />
        <item
            android:drawable="@drawable/a"
            android:state_checked="true" />
        <item
            android:drawable="@drawable/b" />
    </selector>
    

    In the selector above, we reference two drawables, a and b, here's how we create them:

    res/drawable/a.xml - Selected State

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <corners
            android:radius="5dp" />
        <solid
            android:color="#fff" />
        <stroke
            android:width="2dp"
            android:color="#53aade" />
    </shape>
    

    res/drawable/b.xml - Regular State

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <corners
            android:radius="5dp" />
        <solid
            android:color="#fff" />
        <stroke
            android:width="2dp"
            android:color="#555555" />
    </shape>
    

    More on drawables here: http://developer.android.com/guide/topics/resources/drawable-resource.html

    0 讨论(0)
  • 2020-11-22 12:38

    Setting android:background and android:button of the RadioButton like the accepted answer didn't work for me. The drawable image was being displayed as a background(eventhough android:button was being set to transparent ) to the radio button text as

    android:background="@drawable/radiobuttonstyle"
        android:button="@android:color/transparent"
    

    so gave radiobutton as the custom drawable radiobuttonstyle.xml

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Maintenance"
        android:id="@+id/radioButton1"
        android:button="@drawable/radiobuttonstyle"
          />
    

    and radiobuttonstyle.xml is as follows

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

    and after this radiobutton with custom button style worked.

    0 讨论(0)
  • 2020-11-22 12:39

    I realize this is a belated answer, but looking through developer.android.com, it seems that the Toggle button would be ideal for your situation.

    Toggle button image http://developer.android.com/guide/topics/ui/controls/togglebutton.html

    And of course you can still use the other suggestions for having a background drawable to get a custom look you want.

    <ToggleButton 
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_button_background"
        android:textOn="On"
        android:textOff="Off"
        />
    

    Now if you want to go with your final edit and have a "halo" effect around your buttons, you can use another custom selector to do just that.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" > <!-- selected -->
            <shape>
                <solid
                    android:color="@android:color/white" />
                <stroke
                    android:width="3px"
                    android:color="@android:color/holo_blue_bright" />
                <corners
                    android:radius="5dp" />
            </shape>
        </item>
        <item> <!-- default -->
            <shape>
                <solid
                    android:color="@android:color/white" />
                <stroke
                    android:width="1px"
                    android:color="@android:color/darker_gray" />
                <corners
                    android:radius="5dp" />
            </shape>
        </item> 
    </selector>
    
    0 讨论(0)
  • 2020-11-22 12:41

    Use the same XML file format from Evan's answer, but one drawable file is all you need for formatting.

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_button_background"
        android:button="@android:color/transparent"
        android:checked="true"
        android:text="RadioButton1" />
    

    And your separate drawable file:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item android:state_pressed="true" >
             <shape android:shape="rectangle" >
                 <corners android:radius="3dip" />
                 <stroke android:width="1dip" android:color="#333333" />
                 <solid android:color="#cccccc" />            
             </shape>
        </item>
    
        <item android:state_checked="true">
             <shape android:shape="rectangle" >
                 <corners android:radius="3dip" />
                 <stroke android:width="1dip" android:color="#333333" />
                 <solid android:color="#cccccc" /> 
             </shape>
        </item>  
    
        <item>
             <shape android:shape="rectangle"  >
                 <corners android:radius="3dip" />
                 <stroke android:width="1dip" android:color="#cccccc" />
                 <solid android:color="#ffffff" />            
             </shape>
        </item>
    </selector>
    
    0 讨论(0)
提交回复
热议问题