开发者

How to get radio button appearance in checkbox?

开发者 https://www.devze.com 2023-03-02 01:49 出处:网络
I\'m just trying to use different checkboxes whose appearance should looks like an radio button which is开发者_运维知识库 like a round button with a green light inside. I googled this for long time bu

I'm just trying to use different checkboxes whose appearance should looks like an radio button which is开发者_运维知识库 like a round button with a green light inside. I googled this for long time but didn't find a solution, is there any way to do this.

Thanks in advance.


Even better way is to set the style of the CheckBox to match that of a RadioButton.

@android:style/Widget.CompoundButton.RadioButton

Making the CheckBox to look like this

<CheckBox style="@android:style/Widget.CompoundButton.RadioButton" />

It works great! My answer is inspired by this post.


You can change the background (image) of the checkbox (say rather by style than in the layout, to be more elegant) to anything you want, that way you can have a radiobutton-like checkbox.

Please take a look to this short tutorial which will show you the steps to take for achieving your goal.


So as apostolov said, to make your checkbox look like a radio button you can simply setting the checkbox style like so

<CheckBox style="@android:style/Widget.CompoundButton.RadioButton" />

But if like me you want the accent color of your radio button to be different from that of your app's theme. You may want to take the following approach:

set the following attributes on your checkbox:

<CheckBox
      android:button="?android:attr/listChoiceIndicatorSingle"
      android:theme="@style/RadioButtonPink"
      />

The first attribute gives your checkbox the android radio button look, the second theme attribute is to customize the button's accent and normal colors. Inside your styles.xml file define the theme you want to use as so:

<style name="RadioButtonPink">
    <item name="colorAccent">@color/pink</item>
    <item name="colorControlNormal">@color/grey</item>
    <item name="colorControlActivated">@color/grey</item>
</style>

In my case I wanted a pink accent color for when the button is selected and grey otherwise.


Yes, as rakaszeru said, please try above option, or else you can just make your own custom checkbox component just by extending checkbox widget implementing its onDraw() method

0

精彩评论

暂无评论...
验证码 换一张
取 消