如何从MaterialCardView中去除涟漪效果?

24

我如何禁用MaterialCardView的触摸涟漪效果?将clickable属性设置为false或者调整foregroundbackground属性均无效。

我正在使用material support library版本1.1.0-alpha02


可能是MaterialCardView没有水波纹效果的重复问题。 - Martin Zeitler
1
@MartinZeitler 这个问题是相反的!我问的是如何去除涟漪效果。 - Mahozad
4个回答

43

只有rippleColor是一个styleable属性:

<com.google.android.material.card.MaterialCardView
    style="@style/Widget.MaterialComponents.CardView"
    app:rippleColor="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</com.google.android.material.card.MaterialCardView>

注意:它不会从 <androidx.cardview.widget.CardView 中移除涟漪效果。


在MaterialCardView中没有rippleColor属性。那么你该如何移除它?能否请您解释一下? - Vikas singh
@vikassingh 可能是这样的。也许你的依赖关系过时了?试试 1.1.0-beta07。 - behelit

2

在xml中使用以下属性:
app:rippleColor="@android:color/transparent"

或在Kotlin中编程:
cardView.rippleColor = ColorStateList.valueOf(Color.TRANSPARENT)

这些方法可以帮助您将视图的涟漪颜色更改为透明。

1
你还需要将clickable和focusable设置为false,以去除涟漪效果。
<com.google.android.material.card.MaterialCardView
        style="@style/Widget.MaterialComponents.CardView"
        app:rippleColor="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false">
</com.google.android.material.card.MaterialCardView>

-3

你可以只使用CSS来实现:

.mdc-card__primary-action.card__primary-action.mdc-ripple-upgraded {
  &:hover,
  &:focus,
  &:active {
    &.mdc-card__primary-action::before,
    &.mdc-card__primary-action::after {
      background-color: rgba(255, 255, 255, 0) !important;
      opacity: 0 !important;
    }
  }
}

这个问题是关于安卓的。 - David

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