RecyclerView 网格布局

本文深入讲解了RecyclerView中网格布局的应用,特别适用于图片相册浏览场景。通过对比线性布局,详细介绍了网格布局的实现方式及代码修改要点,包括布局文件的调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上面我们介绍的就是?RecyclerView 线性布局的使用,效果还是不错的,一般可以用于浏览个人信息列表操作。接着,我们来看第二种布局形式:网格布局。

? ? 网格布局也是非常常用的,一般用于图片相册的浏览,接下来的工作就是复制粘贴一下原先的代码就可以了,因为适配器里面的代码几乎一个样,我们可以考虑把它封装起来用,不过现在还是怎么方便怎么来。

? ? 对于线性布局来说,网格布局是需要做一定量的修改的,也就是布局文件的修改,我们把旁边的 “我是一只...” 文本给去掉,保留了图片和动物名称,所以布局文件代码修改为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="180dp"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="4dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<ImageView
android:id="@+id/img_recy_item_2_pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop" />

<TextView(http://www.my516.com)
android:id="@+id/tv_recy_item_2_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_recy_item_2_pic"
android:layout_centerInParent="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="18sp" />

</RelativeLayout>
</android.support.v7.widget.CardView>
---------------------

转载于:https://www.cnblogs.com/hyhy904/p/11347006.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值