【问题标题】:Card view material ripple?卡查看材质波纹?
【发布时间】:2015-03-01 23:42:31
【问题描述】:

我正在开发一个材料设计界面。我有一些卡片,我想在点击时产生涟漪效果。我做了谷歌所说的,没有任何反应。这是我的代码:

<android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:clickable="true"
            android:layout_width="fill_parent"
            android:layout_height="250dp"
            android:elevation="5dp"
            android:background="?android:selectableItemBackground">
            >

            <ImageView 
                android:id="@+id/blue_image"
                android:src="@drawable/blue"
                android:layout_alignParentTop="true"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:elevation="8dp"
            />

            <TextView
                android:id="@+id/text_view"
                android:layout_width="match_parent"    
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:text="And this is a blue card!" 
                android:elevation="10dp"
                />

【问题讨论】:

  • 尝试添加android:foreground="?android:attr/selectableItemBackground"
  • @Skizo 现在我可以点击它,它会变得更轻,但不会产生波纹。 (我在 KitKat 顺便说一句)
  • 你看过这个question?
  • 好的,所以没有办法在 KitKat 中制造涟漪?
  • 别这么认为,我猜涟漪只有L....

标签: android material-design


【解决方案1】:

看看我的例子 (API

https://www.youtube.com/watch?v=liure-_hlX0&feature=youtu.be

对于这个演示,我使用了这个库: https://github.com/balysv/material-ripple

只需添加到 build.gradle:

 compile 'com.balysv:material-ripple:1.0.1'

卡片视图的选择器(包装的RelativeLayout):list_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item android:drawable="@color/ripplecolorbackground" android:state_pressed="true"/>
<!-- selected -->
<item android:drawable="@color/ripplecolorbackground" android:state_selected="true"/>
<!-- focused -->
<item android:drawable="@color/ripplecolorbackground" android:state_focused="true"/>
<!-- default -->
<item android:drawable="@color/transparent"/>

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginBottom="5dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="3dp"
    card_view:cardPreventCornerOverlap="true">

    <com.balysv.materialripple.MaterialRippleLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:mrl_rippleColor="@color/rippplecolorlist"
        app:mrl_rippleAlpha="0.10"
        app:mrl_rippleOverlay="false"
        app:mrl_rippleHover="true"
        app:mrl_rippleDimension="1dp"
        app:mrl_rippleDuration="350">

    <RelativeLayout
        android:background="@drawable/list_item_selector"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

            </com.balysv.materialripple.MaterialRippleLayout>
    </android.support.v7.widget.CardView>

颜色:

<color name="ripplecolorbackground">#1A000000</color>
<color name="rippplecolorlist">#26000000</color>

【讨论】:

    【解决方案2】:

    我创建了一个库,允许自定义每个视图而不影响布局以创建波纹效果

    希望对你有帮助 https://github.com/xgc1986/RippleViews/blob/master/docs/RippleDrawableHelper.md

    该示例还使用具有此效果的 cardViews ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2020-08-12
      • 1970-01-01
      • 2016-12-20
      • 2021-06-24
      • 2018-03-22
      • 1970-01-01
      • 2018-05-08
      相关资源
      最近更新 更多