本篇文章同步发表在 HKT 线上教室 部落格,线上影音教学课程已上架至 Udemy 和 Youtube 频道。另外,想追踪更多相关技术资讯,欢迎到 脸书粉丝专页 按赞追踪喔~
范例名称:RecyclerView 卡片式项目布局
开发人员:HKT (侯光灿)
程序语言:Kotlin
开发环境:Android Studio 4.1.2 & Android 11 & Kotlin 1.4.21
授权范围:使用时必须注明出处且不得为商业目的之使用
范例下载点:点我下载
范例名称:新增自定义背景样式
开发人员:HKT (侯光灿)
程序语言:Kotlin
开发环境:Android Studio 4.1.2 & Android 11 & Kotlin 1.4.21
授权范围:使用时必须注明出处且不得为商业目的之使用
范例下载点:点我下载
今天,我们将使用 CardView,将原本连贯的列表资讯改为卡片样式。在整体资讯表达呈现上更容易阅读。
修改 layout/item_view.xml,在原本的布局,最外层多加入 CardView。并且设定 CardView 左、右两边与上面间距皆为10dp。然後调整卡片四周圆角为20dp。移除 RecyclerView 分隔线。
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:paddingBottom="20dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="药局名称"
android:textColor="#424242"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/layout_adult"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_adult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/layout_child"
app:layout_constraintTop_toBottomOf="@+id/tv_name">
<TextView
android:id="@+id/tv_adult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成人口罩"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_adult_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="口罩数量"
android:textSize="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_adult" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/layout_adult"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_name">
<TextView
android:id="@+id/tv_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小孩口罩"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_child_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="口罩数量"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_child" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
若我们想要突显,成人口罩与小孩口罩资讯区块,可以加入背景样式。未来我们可以根据口罩数量来改变颜色,当药局口罩数量充足,背景色为绿色,快售完显示红色,已售完显示灰色。
在 drawable 目录下,按右键 New -> Drawable Resource File 新增档名为 bg_amount_info.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<corners android:radius="4dp" />
</shape>
其中 shape 可以设定属性为 rectangle 为矩形,oval 为圆形或椭圆形,line 为实线或虚线,ring 为环形。
然後在项目布局 layout/item_view.xml 的 layout_adult、layout_child 背景样式设定为 bg_amount_info 并加入周围间距。
android:background="@drawable/bg_amount_info"
android:padding="10dp"
HKT 线上教室
https://tw-hkt.blogspot.com/
Freepik
https://www.freepik.com/
Create a Card-Based Layout
https://developer.android.com/guide/topics/ui/layout/cardview
CardView
https://developer.android.com/reference/androidx/cardview/widget/CardView
Shape drawable
https://developer.android.com/guide/topics/resources/drawable-resource#Shape
後记,今天铁人挑战赛来到了中场,第 15 天,已经过一半了,明天继续加油。
那今天【iThome 铁人赛】就介绍到这边罗~
顺带一提,KT 线上教室,脸书粉丝团,会不定期发布相关资讯,不想错过最新资讯,不要忘记来按赞,追踪喔!也欢迎大家将这篇文章分享给更多人喔。
我们明天再见罗!!!掰掰~
<<: Day15 - 动态 新增/删除 Collection 项目(三) - Tag Helper
>>: Day07 Kibana - Query DSL 语法结构
-ISO 31000 在 ISO 31000 中,风险评监包括三个步骤:风险识别、风险分析和风险评...
不怎麽重要的前言 上一篇介绍了需要与if条件式结合且与回圈控制有关的语法,基本上我们已经把基础的程序...
今天一开始 先来新增spring boot的连线设定 Spring Boot的设定档 applica...
如果想要控制网格项目的放置位置,就要知道怎麽定义项目放置的"行"与"列...
今天试着写了滚动视差的网站 滚动视差主要是靠东西不同的滚动速度来做到前後落差的感觉 先看成品 先做最...