从RadioButton中移除涟漪效果,安卓?

23

我自定义了一个RadioButton样式,为了在右侧添加RadioButton图像

<RadioButton
                android:layout_margin="20dp"
                android:drawableRight="@drawable/custom_btn_radio"
                android:button="@null"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Invite Only"/> 

custom_btn_radio.xml

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

但是,单选按钮的涟漪效果也变大了。我怎样才能从RadioButton中移除涟漪效果?同时还想知道如何自定义涟漪效果?

提前感谢。


4
将单选按钮的背景设置为透明。 - Harin
谢谢@Harry,我设置了android:background="#ffffffff"。还有其他方法可以将背景设置为透明吗? - Riyas PK
@android:color/transparent - Harin
再次感谢你,@Harry。 - Riyas PK
5个回答

62

将 RadioButton 的背景设置为透明

android:background="#00ffffff"
或者
android:background="@android:color/transparent"

1
这对我有用 -> android:backgroundTint="@android:color/transparent" - Miramax Mars
第一个使其变为白色并具有完全不透明度。 - Abhilash Maurya
1
第一个应该是 #00FFFFFF,因为前两个数字是不透明度,FF 表示 100% 不透明,而 00 表示 100% 透明。 - NonGrate
第一个解决方案必须是 #00ffffff 而不是 #ffffffff - E.Akio

3

colorControlHighlightcolorControlActivated设置为透明色对我解决了这个问题:

<style name="transparent_theme">
    <item name="colorControlHighlight">@android:color/transparent</item>
    <item name="colorControlActivated">@android:color/transparent</item>
</style>

2
在“如何自定义涟漪效果”部分,使用Material Components库v1.1.0,默认情况下,背景是一个RippleDrawable,其颜色设置为2种颜色的ColorStateList:启用和选中状态下的26 alpha(#A1 in hex)colorControlActivated,以及默认状态下的alpha白色或黑色,取决于您是否使用浅色主题(#1F000000)或暗色主题(#33FFFFFF)。我通过在调试器中检查MaterialCheckBox的背景来获取此信息。
您可以使用其setColor方法随意覆盖RippleDrawable的颜色。例如,如果您想在使用DayNight主题时仅更改涟漪效果的强调颜色部分,则可以运行此Kotlin代码示例:
private val RIPPLE_ENABLED_CHECKED_STATES = arrayOf(
        intArrayOf(android.R.attr.state_enabled, android.R.attr.state_checked),
        intArrayOf())

fun Int.withAlpha(alpha: Int): Int {
    require(alpha in 0..0xFF)
    return this and 0x00FFFFFF or (alpha shl 24)
}

fun CompoundButton.changeCheckedRippleColor(@ColorInt color: Int) {
    val defaultAlphaColor= if (context.resources.configuration.uiMode and 
                Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
            Color.WHITE.withAlpha(51)
        else
            Color.BLACK.withAlpha(31)
    (background as? RippleDrawable)?.setColor(ColorStateList(RIPPLE_ENABLED_CHECKED_STATES,
                intArrayOf(color.withAlpha(26), defaultAlphaColor)))
}

1
首先,您应该在drawable文件夹中创建一个名为ripple_checkbox_bg.xml的新文件。
    <?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/transparent"> <!-- ripple color -->

    <item android:drawable="@color/transparent"/> <!-- for a transparent background. if you want you can use different color -->

</ripple>

把这段代码放到你的 style.xml 文件中。
<style name="custom_radio_button_style">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/ripple</item>
    <item name="android:button">@drawable/custom_btn_radio</item>
</style>

最后,您可以将此代码放入您的活动XML文件中

<androidx.appcompat.widget.AppCompatCheckBox
                style="@style/custom_radio_button_style"
                android:textColor="@color/black"
                android:text="Ripple effect removed"
             />

-1
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/transparent"> <!-- ripple color -->

    <item android:drawable="your color"/> <!-- normal color -->

</ripple>

1
如何将此项添加到我的 RadioButton? - Riyas PK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接