您的位置:首页 > 其它

卡片view

2015-09-11 09:12 363 查看
美学:cardview更加美观的一个重要原因是平滑渐进

一,cardview内容到groupview之间嵌套一个阴影,造成一种递进效果

二,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的模拟器 可以看到。



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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cardview