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) { }
});
}
}

完成

 

Android中的CardView使用的更多相关文章

  1. 彻底理解 Android 中的阴影

    如果我们想创造更好的 Android App,我相信我们需要遵循 Material Design 的设计规范.一般而言,Material Design 是一个包含光线,材质和投影的三维环境.如果我们想 ...

  2. ANDROID L——RecyclerView,CardView进口和使用(Demo)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控 ...

  3. Android 中的AlertDialog使用自定义布局

    Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...

  4. Android中的LinearLayout布局

    LinearLayout : 线性布局 在一般情况下,当有很多控件需要在一个界面列出来时,我们就可以使用线性布局(LinearLayout)了,  线性布局是按照垂直方向(vertical)或水平方向 ...

  5. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  6. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  7. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  8. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  9. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

随机推荐

  1. C#序列化xml,开发常用

    序列化操作对于开发人员来说最熟悉不过了. 序列化分为:序列化和反序列化. 序列化名词解释:序列化是将对象状态转换为可保持或传输的格式的过程. 与序列化相对的是反序列化,它将流转换为对象.这两个过程结合 ...

  2. Daubechies Wavelet

    The Daubechies wavelets, based on the work of Ingrid Daubechies, are a family of orthogonal wavelets ...

  3. 设计模式05: Prototype 原型模式(创建型模式)

    Prototype 原型模式(创建型模式) 依赖关系的倒置抽象不应该依赖于实现细节,细节应该依赖于抽象.对所有的设计模式都是这样的. -抽象A直接依赖于实现细节b -抽象A依赖于抽象B,实现细节b依赖 ...

  4. windows phone开发必备工具翔

    1.图标设计 http://www.flaticon.com/ http://www.iconfont.cn/ 2.界面设计: 2.1.behance.com    2.2.dribbble.com  ...

  5. .Net Core 项目引用本地类库方式(二)

    上篇文章有详细的介绍.Net Core 项目中引用本地类库通过打包,然后Nugety引用方式,这里再介绍一种引用包的方式

  6. Android Camera相机功能实现 拍照并保存图片

    AndroidManifest.xml <uses-feature android:name="android.hardware.camera"/> <uses- ...

  7. ubuntu14.10,安装ksnapshot(截图软件)

    Linux:ubuntu14.10 ubuntu软件中心对它的描述:KSnapshot captures images of the screen.  It can capture the whole ...

  8. 原码、反码、补码及位操作符,C语言位操作

    计算机中的所有数据均是以二进制形式存储和处理的.所谓位操作就是直接把计算机中的二进制数进行操作,无须进行数据形式的转换,故处理速度较快. 1.原码.反码和补码 位(bit) 是计算机中处理数据的最小单 ...

  9. 80端口被system(pid=4)占用

    1.查看80端口被哪个进程占用,cmd->netstat -ano | findstr 80 2.cmd->tasklist列出当前运行中的进程,或在任务管理器中查看pid为4的进程. 经 ...

  10. 搭建vue环境网站

    https://cli.vuejs.org/zh/guide/installation.html 安装成功vue脚手架 安装成功3.4版本 vue + iview项目构建 https://blog.c ...