【问题标题】:RadioButton set fill color of the circleRadioButton 设置圆的填充颜色
【发布时间】:2019-01-16 13:10:43
【问题描述】:

在我的布局中,我有一个AppCompatRadioButton。使用app:buttonTint,我将按钮设为白色。如果我检查了AppCompatRadioButton,圆圈的填充颜色也是白色的。选中按钮后,如何仅更改圆圈的填充颜色?示例:点击后,圆圈变为白色,圆圈的填充颜色将变为红色。

【问题讨论】:

  • 您是否尝试过使用自定义背景和/或修改主题?我相当有信心你会在那里找到你的解决方案。

标签: android layout colors radio-button


【解决方案1】:

第 1 步如果您只想更改颜色,可以使用主题。

styles.xml

<style name="MyCheckBox" parent="Theme.AppCompat.NoActionBar">
    <item name="colorControlNormal">#d1d1d1</item>   <!-- normal border color change as you wish -->
    <item name="colorControlActivated">#359e1d</item> <!-- activated color change as you wish -->
    <item name="android:textColor">#FFFF3F3C</item> <!-- checkbox text color -->
  </style>

在您的布局中

  <android.support.v7.widget.AppCompatRadioButton
                            android:id="@+id/radioButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:checked="true"
                            android:text="Text"
                            android:theme="@style/MyCheckBox"
                            android:textSize="16dp" />

第 2 步如果你想改变 rabio 按钮的图标

在您的布局中

   <android.support.v7.widget.AppCompatRadioButton
                                android:id="@+id/radioButton"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:checked="true"
                                android:text="Text"
                                android:button="@drawable/cb_selector"
                                android:textSize="16dp" />

您的可绘制对象

 <?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/icon_tick" />
        <item android:state_checked="false" android:drawable="@drawable/icon_un_tick" />
    </selector>

【讨论】:

    【解决方案2】:

    您可以使用自定义图层

    <android.support.v7.widget.AppCompatRadioButton
                            android:id="@+id/radioButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:checked="true"
                            android:text="Text"
                            android:button="@drawable/selector_radio"
                            android:textSize="16dp" />
    

    selector_radio.xml

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

    radio_layer.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/radio_ring_checked"/>
        <item android:drawable="@drawable/radio_ring_unchecked"/>
    
    </layer-list>
    

    radio_ring_checked.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:innerRadiusRatio="4"
        android:shape="ring"
        android:thickness="2dp"
        android:useLevel="false" >
    
        <solid android:color="@color/ash" />
    
        <size
            android:height="30dp"
            android:width="30dp" />
    
    </shape>
    

    radio_ring_unchecked.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:innerRadiusRatio="1000"
        android:shape="ring"
        android:thickness="4dp"
        android:useLevel="false" >
    
        <solid android:color="@color/colorPrimaryButton" />
    
        <size
            android:height="18dp"
            android:width="18dp" />
    
    </shape>
    

    【讨论】:

    • 非常好的和有用的答案。工作非常好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 2013-07-01
    相关资源
    最近更新 更多