【问题标题】:Remove ripple effect from RadioButton, android?从 RadioButton,android 中删除波纹效果?
【发布时间】:2021-09-28 00:06:28
【问题描述】:

我自定义了一个 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 中移除涟漪效应?还有如何自定义涟漪效果?

提前谢谢你。

【问题讨论】:

  • 只需将 RadioButton 背景设置为透明即可。
  • 谢谢@Harry 我设置了 android:background="#ffffffff"。有没有其他方法可以设置背景透明?
  • @android:color/transparent
  • 再次感谢@Harry

标签: android


【解决方案1】:

将 RadioButton 背景设为透明

android:background="#ffffffff"

android:background="@android:color/transparent"

【讨论】:

  • 这对我有用 -> android:backgroundTint="@android:color/transparent"
  • 第一个使它成为完全不透明的白色
  • 第一个应该是#00FFFFFF,因为前两个数字是不透明度,FF neams 100% 不透明,而 00 表示 100% 透明
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在“如何自定义波纹效果”部分,使用 Material Components 库 v1.1.0,默认情况下,背景是 RippleDrawable,其颜色设置为 ColorStateList,有 2 种颜色:A 26 alpha (#A1十六进制)colorControlActivated 处于启用和检查状态,默认状态为白色或黑色,具体取决于您是浅色(#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)))
    }
    

    【讨论】:

      【解决方案4】:

      首先你应该在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"
                   />
      

      【讨论】:

        【解决方案5】:
        <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>
        

        【讨论】:

        • 如何将它添加到我的 RadioButton 中?
        猜你喜欢
        • 2021-07-21
        • 1970-01-01
        • 2021-03-03
        • 1970-01-01
        • 2017-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-12
        相关资源
        最近更新 更多