简体   繁体   English

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

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

Background背景

Before CardView was introduced, I made some selectors on my app to mimic cards, and let the user also choose which theme to use for the app (some prefer a dark theme) :在 CardView 被引入之前,我在我的应用程序上做了一些选择器来模仿卡片,并让用户也选择应用程序使用哪个主题(有些人更喜欢深色主题):

在此处输入图片说明

The problem问题

I wanted to make it look&work more natively, so I tried using CardView.我想让它看起来和工作更原生,所以我尝试使用 CardView。

Sadly, I fail to understand how to set the CardView have a clickable&checkable effect (the native one of each platform, maybe with a different color), and also have the ability to set it a dark theme.遗憾的是,我不明白如何设置 CardView 具有可点击和可检查的效果(每个平台的原生效果,可能具有不同的颜色),并且还能够将其设置为深色主题。

The questions问题

  1. How do I make a CardView have a clickable effect?如何使 CardView 具有可点击效果? On Lollipop it's a ripple effect.在棒棒糖上,这是一种涟漪效应。 On previous ones it's full color changing within the boundaries of the CardView.在以前的版本中,它在 CardView 的边界内全色变化。 I'd also like to customize the color of the clickable effect, and let it also be checkable.我还想自定义可点击效果的颜色,并让它也可以检查。

  2. How do I make a dark-theme CardView ?如何制作深色主题 CardView ?

You have to use the CardView.Dark style for the dark-theme CardView.对于深色主题的 CardView,您必须使用 CardView.Dark 样式。 You can also just use the colors as mentioned in the 11th and 12th comments of this bug .您也可以仅使用此错误的第 11 条和第 12 条评论中提到的颜色。

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

But after release of Android Support Library, revision 23.2.1 (March 2016) This functionality is added.但在 Android 支持库发布后,修订版23.2.1 (2016 年 3 月)添加了此功能。

Add dark theme for CardView为 CardView 添加深色主题

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

Example:例子:

Add below attribute to your cardview将以下属性添加到您的 cardview

style="@style/CardView.Dark"

as shown here如图所示

<?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