[Android Studio 30天自我挑战] CradView布局练习

这篇要透过CardView来制作一个简易的清单

首先要在Gradle Scripts/build gradle(.app)里增加CardView的功能

implementation 'androidx.cardview:cardview:1.0.0'

因为要使用的CardView有多个,所以外层要用ScrollView来帮助滚动才可以向下滑
xml范例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    android:background="#ffff"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="法式小点心"
                android:textColor="#000"
                android:textSize="30dp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <androidx.cardview.widget.CardView
                    //这里可以设定CardView的外观
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginTop="20dp"
                    app:cardCornerRadius="15dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#FFA042"
                        android:padding="10dp">

                        <TextView
                            android:id="@+id/t1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="10dp"
                            android:text="可丽露"
                            android:textColor="#ffff"
                            android:textSize="30dp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/t2"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_below="@id/t1"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="3dp"
                            android:text="是一种小型的法式甜点,表层则是硬脆又厚实的褐色焦糖外壳,内部是半融化状的蛋糕糊,散发着酒香和香草味。"
                            android:textColor="#ffff" />

                    </RelativeLayout>

                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginTop="20dp"
                    app:cardCornerRadius="15dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#FF9797"
                        android:padding="10dp">

                        <TextView
                            android:id="@+id/t3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="10dp"
                            android:text="马卡龙"
                            android:textColor="#ffff"
                            android:textSize="30dp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/t4"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_below="@id/t3"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="3dp"
                            android:text="是一种用色彩缤纷绚丽的法国甜品,外壳坚硬但易碎,内陷黏稠扎实"
                            android:textColor="#ffff" />

                    </RelativeLayout>

                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginTop="20dp"
                    app:cardCornerRadius="15dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#02F78E"
                        android:padding="10dp">

                        <TextView
                            android:id="@+id/t5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="10dp"
                            android:text="玛德莲"
                            android:textColor="#ffff"
                            android:textSize="30dp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/t6"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_below="@id/t5"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="3dp"
                            android:text="是一种传统的贝壳形状的小蛋糕,来自於法国东北部洛林大区的两个市镇科梅尔西和利韦尔丹。"
                            android:textColor="#ffff" />

                    </RelativeLayout>

                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginTop="20dp"
                    app:cardCornerRadius="15dp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#46A3FF"
                        android:padding="10dp">

                        <TextView
                            android:id="@+id/t7"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="10dp"
                            android:text="舒芙蕾"
                            android:textColor="#ffff"
                            android:textSize="30dp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/t8"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_below="@id/t7"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="3dp"
                            android:text="是一种源自法国的甜品,经烘焙後质感轻而蓬松"
                            android:textColor="#ffff" />

                    </RelativeLayout>

                </androidx.cardview.widget.CardView>

            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

这样就完成简单的外观设计了
https://ithelp.ithome.com.tw/upload/images/20211011/20139258OpMv9HAszC.png


<<:  Day28:阿赖耶识

>>:  【Day 26】Google Apps Script - API Blueprint 篇 - Apiary 介面介绍

k8s react 把变数(ex:api url)写进ConfigMap

【YC的寻路青春】 react上k8s的部分 前提 如果放在.env里面 react再build的时...

【Day01】楔子-关於永丰金融APIs

iT邦帮忙一直以来都是我查询技术问题的好夥伴;而铁人赛为IT界名闻遐迩的年度盛事。 在友人极力鼓吹报...

安全评监(Security Assessment)

-ISO 31000 在 ISO 31000 中,风险评监包括三个步骤:风险识别、风险分析和风险评...

建立Endpoint执行二次开发

上一篇我们已经建立好模型, 而且也上传一张图片验证推论的结果符合我们的预期. 接下来我们要把mode...

Swift 新手- 资讯安全讯息加密

社群软件的用户间交谈讯息是如何做到资讯安全保护? 关於端对端加密 隐私与安全对我们来说极度重要,因此...