Android 5.0 CardView 应用

CardView 属于Support v7 里面的新的Widget.  扩展于FrameLayout,

UI显示主要包括

1.边框圆角

2.有阴影Shadow

用来突出个性,比如展览,相册等。

主布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.gaofeng.recyclerviewdemo.CardViewActivity"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@android:color/white"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="24dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/cat0"
            android:layout_centerHorizontal="true"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/text_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img"
            android:layout_centerHorizontal="true"
            android:text="This is a card view"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin" />
    </RelativeLayout>
 </android.support.v7.widget.CardView>

    <SeekBar
        android:layout_below="@+id/card_view"
        android:id="@+id/seek1"
        android:layout_marginTop="10dp"
        android:layout_width="150dp"
        android:layout_height="30dp"
        />

    <SeekBar
        android:layout_below="@+id/seek1"
        android:id="@+id/seek2"
        android:layout_marginTop="10dp"
        android:layout_width="150dp"
        android:layout_height="30dp"
        />
</RelativeLayout>

重要的几个属性:

card_view:cardCornerRadius="10dp"  圆角的半径

card_view:cardElevation="24dp"         阴影shadow

SeekBar用来测试这2个值的。

package com.example.gaofeng.recyclerviewdemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;

public class CardViewActivity extends ActionBarActivity {

    CardView cardView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view);

        cardView = (CardView) findViewById(R.id.card_view);

        SeekBar seek1 = (SeekBar) findViewById(R.id.seek1);
        SeekBar seek2 = (SeekBar) findViewById(R.id.seek2);

        seek1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                   cardView.setCardElevation(i);//shadow
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        seek2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                    cardView.setRadius(i);//圆角大小设置
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

有的5.0的模拟器看不到 Shadow 建一个Nexus 1080的模拟器 可以看到。

拖SeekBar 大点 有明显的 阴影和圆角。

build.gradle 加入依赖

dependencies {
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:recyclerview-v7:21.0.+'
    compile 'com.android.support:cardview-v7:21.0.+'
}
时间: 2024-08-03 14:46:43

Android 5.0 CardView 应用的相关文章

Android L5.0 CardView与RecycleView

http://doc.okbase.net/a396901990/archive/107839.html http://www.tuicool.com/articles/3IziIba http://www.imooc.com/learn/215

ym—— Android 5.0学习之CardView

前言 CardView顾名思义,就是想卡片一样的控件,如图: Android 5.0之前,我们有两种方案做出这种效果: 1.通过设置背景图 2.设置配置Shape文件 而现在我们需要麻烦美工MM,也不需要配置麻烦的Shape文件,只需要简单的设置几个属性即可,那就是用我们CardView CardView CardView继承了FrameLayout类,并让你在里面的卡片中(显示)有跨平台一致性的外观.CardView控件可以有阴影和圆角(效果). 要创建具有阴影效果的卡片,可以使用card_v

android 5.0新特性学习--CardView

CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. CardView的属性: elevation --CardView的Z轴阴影; cardBackgroundColor--CardView的卡片颜色; cardConerRadius -- CardView卡片的四角圆角矩形程度; 下面xml文件中即表示这个card控件为:矩形程度为8dp,背景颜色为黑色的卡片. <androi

[翻译]Android 5.0之应用中实现材料设计—Material Design

上午的时候在刷Google+,看到了Abraham Williams转发了一篇强文,是Android Developers网站新发的一篇博客—Implementing Material Design in your Android App.觉得很前卫,对于新发布的Android版本号Android 5.0是一个很好的学习和了解的机会,所以就花了些时间把它翻译了下来,希望对自己.对其它人有所启发. 因为翻译Android开发博客和API也只是业余爱好,水平有限,其中不免有不准确的地方,所以把原文地

Android RecyclerView 和 CardView简单使用

Android 5.0之后Android新增加的两个UI控件RecyclerView,CardView. RecyclerView可以看出是ListView的加强版,能够更加灵活的使用.支持动画等 CardView则是Google提供的一个卡片式视图组件,可以定义如边角的弧度.阴影等属性.从本质上看,可以将CardView看做是FrameLayout在自身之上添加了圆角和阴影效果. 要使用这两个UI控件我们需要先在Android Studio 中添加相应的库: 之后要有两个布局文件: main_

ANDROID L——RecyclerView,CardView导入和使用(Demo)

转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简介: 这篇文章是ANDROID L--Material Design详解(UI控件)的一个补充或者说是应用实例,如果有时间建议大家稍微浏览一下上篇文章. 本文主要介绍Android L新增加的两个UI控件RecyclerView,CardView的导入和使用. RecyclerView是ListView的升级版 CardView则是Google提供的一个卡片式视图组件 本例就是使用Re

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

简单介绍: 这篇文章是ANDROID L--Material Design具体解释(UI控件)的一个补充或者说是应用实例,假设有时间建议大家略微浏览一下上篇文章. 本文主要介绍Android L新添加的两个UI控件RecyclerView,CardView的导入和使用. RecyclerView是ListView的升级版 CardView则是Google提供的一个卡片式视图组件 本例就是使用RecyclerView来展示多个CardView的一个小样例.先看下效果图: 导入RecyclerVie

Android L——RecyclerView,CardView的导入和使用

本文主要介绍Android L新增加的两个UI控件RecyclerView,CardView的导入和使用. RecyclerView:ListView的升级版,它提供了更好的性能而且更容易使用.该控件是一个可以装载大量的视图集合,并且可以非常效率的进行回收和滚动.当你list中的元素经常动态改变时可以使用RecyclerView控件.它提供了如下两个功能: 1.为每个条目位置提供了layout管理器(RecyclerView.setLayoutManager) 2.为每个条目设置了操作动画(Re

[翻译]AppCompat v21 — Android 5.0之前版本设备的Material Design实现

博客原文地址:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html,要想打开,请FQ. Android 5.0 SDK在上周五发布,它以新的UI控件.材料设计-我们专注于良好设计的可视化语言为特色.为了能够让你把最新的设计应用到之前的Android平台上,我们扩展了我们的支持包,包括具有较大更新的AppCompat,同样还有新的RecyclerView.CardView和