使用CardView实现卡片式布局,CardView是appcompat-v7库提供的,实际上CardView也是一个FrameLayout,只是额外提供了圆角和阴影等效果,看上去有立体感觉。
效果图:
为了充分利用屏幕,这里使用了RecyclerView来展示。
这里我们要使用Glide来加载图片,所以要先添加一下依赖:
implementation 'com.github.bumptech.glide:glide:3.7.0'
下面就是RecyclerView的具体实现,实现的文件如下:
- MainActivity的布局文件
- layout_fruit_item.xml 每个item的布局
- Fruit类
- FruitAdapter 适配器
- MainActivity
MainActivity的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.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" />
<android.support.v7.widget.RecyclerView
android:id="@+id/main_rcy"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
layout_fruit_item.xml 每个item的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=