Android中的CardView使用

Android 5.0 版本中新增了CardView,CardView继承自FrameLayout类,并且可以设置圆角和阴影,使得控件具有立体性,也可以包含其他的布局容器和控件。

1.配置build.gradle

如果SDK低于5.0,我们仍旧要引入v7包。在build.gradle 中加入如下代码已自动导入 support-v7包。记得配置完再重新Build一下工程。

compile 'com.android.support:appcompat-v7:22.2.1‘
compile 'com.android.support:cardview-v7:22.1.0'

2.使用CardView实现如下效果:

 

布局如下:

<?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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/cv_cardview"
        app:cardCornerRadius="20dp"
        app:cardElevation="20dp"
        android:layout_centerInParent="true">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/aa"
            android:scaleType="centerInside"/>
    </android.support.v7.widget.CardView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <SeekBar
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/sb_1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="控制圆角大小"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <SeekBar
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/sb_2"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="控制阴影大小"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <SeekBar
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/sb_3"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="控制图片间距大小"/>
    </LinearLayout>



</LinearLayout>

重要属性:

app:cardCornerRadius 设置圆角的半径
app:cardElevation 设置阴影的半径
其它属性:
        app:cardBackgroundColor=""设置背景色
        app:cardMaxElevation="" 设置Z轴最大高度值
        app:cardUseCompatPadding="" 是否使用CompatPadding
        app:cardPreventCornerOverlap="" 是否使用PreventCornerOverlap
        app:contentPadding="" 内容的Padding
        app:contentPaddingTop="" 内容的上Padding
        app:contentPaddingLeft="" 内容的左Padding
        app:contentPaddingRight="" 内容的右Padding
        app:contentPaddingBottom="" 内容的下Padding

java代码:

package com.example.cardviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

   /* app:cardBackgroundColor=""设置背景色
    app:cardMaxElevation="" 设置Z轴最大高度值
    app:cardUseCompatPadding="" 是否使用CompatPadding
    app:cardPreventCornerOverlap="" 是否使用PreventCornerOverlap
    app:contentPadding="" 内容的Padding
    app:contentPaddingTop="" 内容的上Padding
    app:contentPaddingLeft="" 内容的左Padding
    app:contentPaddingRight="" 内容的右Padding
    app:contentPaddingBottom="" 内容的下Padding*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CardView mCardview = (CardView) findViewById(R.id.cv_cardview);
        SeekBar sb_1 = (SeekBar) findViewById(R.id.sb_1);
        SeekBar sb_2 = (SeekBar) findViewById(R.id.sb_2);
        SeekBar sb_3 = (SeekBar) findViewById(R.id.sb_3);

        sb_1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mCardview.setRadius(i);//设置圆角半径
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        sb_2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mCardview.setCardElevation(i);//设置阴影半径
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        sb_3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mCardview.setContentPadding(i, i, i, i);//设置cardView中子控件和父控件的距离
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

完成

 
posted on 2018-04-19 18:15  巫山老妖  阅读(29390)  评论(2编辑  收藏  举报