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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3576507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:49:41  来源:igfitidea点击:

Is it possible to change the radio button icon in an android radio button group

androidradio-button

提问by yamspog

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.

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

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.

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

回答by Konstantin Burov

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

是的,您可能必须在 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' here should be a stateful drawable, radio.xml:

这里的“radio”应该是一个有状态的 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.

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

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.

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

回答by Chirag

You can put custom image in radiobutton like normal button. for that create one XML file in drawable folder e.g

您可以像普通按钮一样将自定义图像放在单选按钮中。为此在可绘制文件夹中创建一个 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> 

Here you can use 3 different images for radiobutton

在这里,您可以为单选按钮使用 3 个不同的图像

and use this file to RadioButton like:

并将此文件用于 RadioButton,例如:

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

回答by topcbl

The easier way to only change the radio button is simply set selector for drawable right

仅更改单选按钮的更简单方法是将选择器设置为可绘制右侧

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

And the selector is:

选择器是:

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

That's all

就这样

回答by shweta jariya

yes....` from Xml

是的......`来自Xml

android:button="@drawable/yourdrawable" 

and from Java

和来自 Java

myRadioButton.setButtonDrawable(resourceId or Drawable);

`

`

回答by Abhishek Kumar

In case you want to do it programmatically,

如果您想以编程方式执行此操作,

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

回答by kolunar

Here's probably a quick approach,

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

enter image description here

在此处输入图片说明

With two icons shown above, you shall have a RadioGroupsomething like this

上面显示了两个图标,你应该有一个RadioGroup这样的东西

enter image description here

在此处输入图片说明

  • change the RadioGroup's orientation to horizontal
  • for each RadioButton's Properties, try giving the icon for Buttonunder CompoundButton,
  • adjust the Padding and size,
  • and set the Background attribute when checked.
  • RadioGroup的方向更改为水平
  • 对于每个RadioButton的属性,请尝试为Buttonunder提供图标CompoundButton
  • 调整填充和大小,
  • 并在选中时设置背景属性。