这篇文章主要介绍一下我自己对于CardView和Material Design学习的过程。学习途径主要是通过书本和网上资料学习。
效果图:
说明一下,通过下拉可以让我们宇宙的图片拉伸延展出来,通过上滑,可以让我们宇宙图片逐渐变换成ToolBar样式。
一.什么是Material Design:
Material Design,中文名:材料设计语言,是由Google推出的全新的设计语言,谷歌表示,这种设计语言旨在为手机、平板电脑、台式机和“其他平台”提供更一致、更广泛的“外观和感觉”。
个人感觉这样的界面设计可以使得我们的APP界面更加的统一和美观。
二.什么是CardView:
卡片布局,从名字中就能感受到界面的划分以卡片形式为主,并且通过其属性设置,可以更大程度上满足我们对于卡片界面的设计。
三.学习过程:
首先我们先添加依赖:
compile 'com.android.support:recyclerview-v7:25.1.1'
compile 'com.android.support:cardview-v7:25.1.1'
compile 'com.github.githubwing:ByeBurger:1.2.3'
这部分依赖是用于cardview和recyclerview包括coordinationlayout的依赖。
然后看一下布局文件:
recycler_view_item:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
android:clickable="true"
app:cardElevation="5dp"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="center"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:layout_gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
可以看到,我们外层布局采用的是cardview布局,我们设置了它的阴影,圆角和边距,然后下面的控件就是具体item中显示的控件了。
这个布局文件也是我们图一种Recycler的Item的布局样式。
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.adm