Android 5.0 CardView 应用

来源:互联网 发布:淘宝客服骂人投诉电话 编辑:程序博客网 时间:2024/05/24 07:32


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.+'}






10 2