68

任何人都可以向我解释如何在 CardView 中实现在 Google I/O 2014 上展示的一些视觉触摸反馈。

这是我在 XML 中使用 CardView 的方式,可能我缺少一些小东西,所以我只是想知道是否有人可以帮助我?

<!-- A CardView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CardView_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp" 
    card_view:cardCornerRadius="4dp"
    android:elevation="2dp">

    <LinearLayout
        android:id="@+id/LinearLayout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:onClick="RunSomeMethod"">

    <!-- Main CardView Content In Here-->

    </LinearLayout> </android.support.v7.widget.CardView>
4

5 回答 5

185

API 11+

添加android:foreground="?android:attr/selectableItemBackground"到您的CardView元素。

API 9+

添加android:foreground="?selectableItemBackground"到您的CardView元素。


编辑:源自中心而非触摸点的波纹是一个已知错误,已修复

于 2014-06-29T10:32:02.703 回答
35

要正确绘制棒棒糖前和棒棒糖选择,您可以使用以下方法(想法是使用带有圆角的选择器的插图可绘制用于棒棒糖前的圆角 - 下面的示例使用自定义颜色,您可以将它们更改为默认颜色)

android:foreground="@drawable/card_foreground"

棒棒糖后

可绘制-v21/card_foreground.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#20000000"
        android:drawable="@drawable/card_foreground_selector" />

drawable-v21/card_foreground_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#18000000"/>
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
        </shape>
    </item>
</selector>

前棒棒糖

drawable/card_foreground.xml (您需要根据卡片的高度调整插入值)

<inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/card_foreground_selector"
    android:insetLeft="2dp"
    android:insetRight="2dp"
    android:insetTop="4dp"
    android:insetBottom="4dp"/>

可绘制/card_foreground_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#18000000"/>
            <corners android:radius="@dimen/card_radius" />
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
            <corners android:radius="@dimen/card_radius" />
        </shape>
    </item>
</selector>
于 2015-05-12T13:37:21.450 回答
10

这对我有帮助

背景:

赞成的CardView忽略只能是颜色。边框和阴影实际上是背景的一部分,因此您无法自行设置。android:backgroundapp:cardBackground

解决方案:

使布局内部CardView可点击而不是卡片本身。您已经编写了此布局所需的两个属性:

android:clickable="true"
android:background="?android:selectableItemBackground"
于 2016-03-29T18:24:59.380 回答
3

这是我的解决方案。它对棒棒糖+使用波纹,对棒棒糖前的设备使用静态前景。

<android.support.v7.widget.CardView
    ...
    android:foreground="?android:attr/selectableItemBackground">
于 2015-10-10T19:41:05.250 回答
0

请使用com.google.android.material.card.MaterialCardView支持androidx.cardview.widget.CardView将提供开箱即用的功能。

背景:https ://developer.android.com/jetpack/androidx/releases/cardview是取代https://developer.android.com/topic/libraries/support-library/packages#v7-cardview和 Material Components的新基础https://developer.android.com/reference#other-libraries建立在 androidx.cardview 之上,使用根据https://material.io/components/cards/android#card anatomy 定义的前景和波纹颜色 ... 所以检查您是否自定义?attr/colorSurface,?attr/colorOnSurfaceapp:cardForegroundColor设置相互匹配的值以获得可见的更改

所以,这主要是清理“hacky解决方案” https://stackoverflow.com/a/30192562/529977触及基础的地方

但是:听起来像https://github.com/material-components/material-components-android/issues/1095 ‍♂️ 这样的新问题,而且代码文档似乎有点奇怪https://github .com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/card/MaterialCardView.java#L303

于 2021-03-18T20:37:49.370 回答