【Android】卡片式布局 && 滚动容器ScrollView


三三要成为安卓糕手

一:卡片式布局

先用,上手一遍,然后在一点点剖析

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"

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


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:cardCornerRadius="24dp"
        app:cardElevation="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="12dp">

            <ImageView
                android:id="@+id/iv_avatar"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@drawable/icon_logo"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:text="老孙发来1条消息"
                android:textSize="20sp"
                app:layout_constraintStart_toEndOf="@id/iv_avatar"
                app:layout_constraintTop_toTopOf="@id/iv_avatar" />

            <TextView
                android:id="@+id/tv_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="16:41"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="落魄谷中寒风吹,春秋蝉鸣少年归,荡魂山处石人泪,定仙游走魔向北"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/iv_avatar"
                app:layout_constraintTop_toBottomOf="@id/tv_title" />


        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

效果:

这里为了对子控件更好的进行管理,我们内嵌套一个子布局Constrainlayout

1:elevation-卡片阴影高度

elevation ˌelɪˈveɪʃn

  • elevation:常见意思是 "海拔;高度;提升"

  • "cardElevation" 卡片阴影高度 一般数值在阴影部分10dp左右

我们可以添加一个边距,让card的阴影部分露出来


2:Corner-角

Corner ˈkɔːnə®

方框四个尖尖设置成圆角,一般数值在10~16dp之间

3:优化边距

4:宽度掌控权

宽度设置为0dp后,才会被交给我们的ConstraintLayout去管理,实际开发过程中需要去注意的!!吃过好几次亏了

ellipsis跟maxLines搭配使用 ɪˈlɪpsɪs 省略 ellipsize 后缀 -ize 常用于将名词 / 形容词转化为动词, 可理解为 "使(文本)以省略号形式呈现"

二:源码分析

1:来源

我们的CardVIew是androidx中提供的,一些老的工程没有导入androidx依赖的话,需要我们手动添加

material 材料

这个material里就会给我们提供非常丰富的样式,比如我们的CardView

2:类所处位置

三:ScrollView滚动布局

1:问题引入

字太多,占据很多高度------如下左图;

或者一行显示不满,超了,如下右图;于是引出滚动布局ScrollView耶


2:竖直、水平滚动

应用起来非常的简单,需要注意的一点是在LinearLayout父布局下使用时注意orientation(方向)的设置

xml 复制代码
<!--    竖直滚动-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="100dp">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:text="As some countries expand operations in Antarctica, the United States maintains three year-round stations on the continent with more than 1,000 people during the southern hemisphere's summer, including those at the Amundsen-Scott station, built in 1956 at an elevation of 9,301 feet on a plateau at the South Pole."
                android:textSize="42sp"/>
        </ScrollView>
xml 复制代码
<!--    水平滚动-->
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="AADSODIAOSNOSAINOISAOBsadasdsadsadsadsadsaVAIUVBAOAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIUBVODABVOIDABVOI"
            android:textSize="60sp" />
    </HorizontalScrollView>

3:只能有一个子下级

scrollview只允许有一个子下级 很重要!!!

内部嵌套一个线性布局即可。不同的TextView,用背景色或者文字颜色区分即可

xml 复制代码
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:text="AADSODVAIUVBAOUBVODINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIUBVODABVOIDABVOI"
                android:textSize="60sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:text="AADSODVBAOAADSOOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIUBVODABVOIDABVOI"
                android:textColor="@color/my_blue"
                android:textSize="60sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:text="AADSODAADSODIBVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIAADSODIAOSNOSAINOISAOBVAIUVBAOUBVODABVOIDABVOIUBVODABVOIDABVOI"
                android:textColor="@color/purple"
                android:textSize="60sp" />

        </LinearLayout>


    </ScrollView>

4:scrollbars

"scrollbars" 指的是滚动条 单词意思拆开来理解

商业开发中,一般不希望显示这个滚动条,

相关推荐
顺风尿一寸1 分钟前
记一次 Spring AOP 与定时任务引发的死锁排查
java
用户298698530142 分钟前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
2501_916007472 分钟前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
Revolution613 分钟前
一段 JavaScript 代码执行时,到底发生了什么
前端·javascript
用户77283104908404 分钟前
krono-job:零侵入、单二进制交付的分布式任务调度平台(开源)
后端
智能起源4 分钟前
关于元素层级过多,导致压祯的问题(使用winform或者WPF应用的webview组件嵌套网页时,动效卡顿问题)
前端
用户446139430275 分钟前
从单体到微服务:我们项目的拆分思路和踩坑记录
java
Conan在掘金11 分钟前
鸿蒙报错速查:Function lacks ending return statement,返回路径漏 return 就炸,根因 + 真解法
后端
黄林晴35 分钟前
Nearby Connections 重大变更:不再自动开启 Wi-Fi / 蓝牙,适配指南
android
TDengine (老段)35 分钟前
TDengine 免费版说明
java·大数据·数据库·物联网·时序数据库·tdengine