开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 21 天,点击查看活动详情
前言
之前的文章(安卓UI组件开发学习——抽屉布局DrawerLayout和标题栏Toolbar - 掘金 (juejin.cn))我们使用Material库的组件组合使用得到了一个全新的滑动菜单,本文就是开始将一些组件结合使用获取一个卡片式布局,就和电商的展示图一样。
正文
CoordinatorLayout的使用
首先我们先要学习一个很重要的组件——CoordinatorLayout,这是一个加强版的FrameLayout,但是拥有一些额外的Material能力,使用它可以监听所有子控件的各种事件,还能为我们做出最为合理的响应,比如之前我们看到的悬浮按钮如果被Snackbar的弹出提示遮挡,我们的CoordinatorLayout就会自动将内部的悬浮控件(兔兔按钮——安卓悬浮按钮 - 掘金 (juejin.cn))向上移,以避免被遮挡,布局中使用这个控件非常简单,直接把我们原来布局里写的FrameLayout替换即可:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways|snap" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/rabbit_logo" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
其实这个布局是我们Material库中是非常重要的存在,它作为顶层布局可以协调我们子View之间交互,所以我们称它为协调者布局,但是一用上它,我们需要考虑子布局自身属性效果是否被影响,这需要我们比较熟悉CoordinatorLayout的实现过程,这里不做过多展开。
MaterialCardView的使用
写好CoordinatorLayout后我们就可以写子布局了,我们采用MaterialCardView中放一个 TextView,这样TextView就能显示在一个卡片中,app:cardCornerRadius可以调整这个卡片的圆角大小,android:elevation是控制高度效果,也就是阴影(高度越大,投影效果越淡,投影范围越大,反之亦然),调整好参数大小展示我们单个卡片的样式:
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="35dp"
app:cardCornerRadius="4dp"
android:elevation="5dp">
<TextView
android:id="@+id/vText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
</com.google.android.material.card.MaterialCardView>
我们看到MaterialCardView布局有一个子布局TextView,这样这个TextView就会显示在一张卡片中。
AppBarLayout && SwipeRefreshLayout
在Activity的布局中,我们也要修改对应代码
这里的AppBarLayout用于防止Toolbar被遮挡,SwipeRefreshLayout添加下拉刷新功能。
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
除此以外,我们在Activity的布局中,增加了一个RecyclerView,这样我们就能让我们的卡片放在列表中滑动展示,当然,我们也需要去添加新的依赖使我们能够使用SwipeRefreshLayout下拉刷新:
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
//下拉刷新
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.15.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.15.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
这里还没讲完,需要更新我们的RecyclerView在Activity中adapter与子项实现以及下拉刷新的实现方法
总结
卡片式布局,这是一种布局组件组合在一起形成新的效果的简化说法,其实就是我们的列表里面增加了很多卡片,它们都有Material的特性(圆角和阴影使其有立体感,让页面不再扁平),组合在一起就符合了Material的设计思想。