繁体   English   中英

如何使 CardView 具有可点击和可检查的效果,以及如何使其成为深色主题?

[英]How to make a CardView have a clickable&checkable effect, and how to make it dark themed?

背景

在 CardView 被引入之前,我在我的应用程序上做了一些选择器来模仿卡片,并让用户也选择应用程序使用哪个主题(有些人更喜欢深色主题):

在此处输入图片说明

问题

我想让它看起来和工作更原生,所以我尝试使用 CardView。

遗憾的是,我不明白如何设置 CardView 具有可点击和可检查的效果(每个平台的原生效果,可能具有不同的颜色),并且还能够将其设置为深色主题。

问题

  1. 如何使 CardView 具有可点击效果? 在棒棒糖上,这是一种涟漪效应。 在以前的版本中,它在 CardView 的边界内全色变化。 我还想自定义可点击效果的颜色,并让它也可以检查。

  2. 如何制作深色主题 CardView ?

对于深色主题的 CardView,您必须使用 CardView.Dark 样式。 您也可以仅使用此错误的第 11 条和第 12 条评论中提到的颜色。

这是在谷歌上要求的https://code.google.com/p/android/issues/detail?id=194497

但在 Android 支持库发布后,修订版23.2.1 (2016 年 3 月)添加了此功能。

为 CardView 添加深色主题

将支持库更新为Android Support Library to 23.2.1

例子:

将以下属性添加到您的 cardview

style="@style/CardView.Dark"

如图所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cards"
        style="@style/CardView.Dark"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="4dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:padding="10dp">

            <TextView
                android:id="@+id/usersName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:paddingTop="10dp"
                android:text="Username"
                android:textColor="#FFFFFF" />

            <TextView
                android:id="@+id/others"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/usersName"
                android:layout_centerVertical="true"
                android:paddingTop="10dp"
                android:text="Others"
                android:textColor="#FFFFFF" />

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM