1. 程式人生 > >CardView實現列表效果(含圓角陰影等效果)

CardView實現列表效果(含圓角陰影等效果)

一、新增依賴

dependencies {
    compile 'com.android.support:cardview-v7:26.1.0'
}

點選右上角Sync Now

不會新增依賴或新增依賴出現錯誤,請見這篇文章

二、簡單效果實現

1、  cardElevation:設定陰影的大小

2、  cardBackgroundColor:卡片佈局的背景顏色

3、  cardCornerRadius:卡片佈局的圓角的大小

4、  conentPadding:卡片佈局和內容之間的距離

5、android:clickable="true"

6、android:foreground="?android:attr/selectableItemBackground"設定點選的水波紋效果

7、cardUseCompatPadding:是否設定內邊距

CardView簡單效果

程式碼如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.administrator.cardvieweaxmple.MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@null"
        app:cardCornerRadius="10dp"
        app:cardElevation="20dp"
        app:cardUseCompatPadding="true">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>