【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" 指的是滚动条 单词意思拆开来理解

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

相关推荐
超级小忍8 分钟前
Spring Boot 配置文件常用配置属性详解(application.properties / application.yml)
java·spring boot·后端
麦兜*9 分钟前
基于Spring Boot的审计日志自动化解决方案,结合SpEL表达式和AOP技术,实现操作轨迹自动记录,并满足GDPR合规要求
java·jvm·spring boot·后端·spring·spring cloud·maven
Victor3569 分钟前
MySQL(167)如何理解MySQL的Redo Log和Undo Log?
后端
Victor35610 分钟前
MySQL(168)MySQL如何实现崩溃恢复?
后端
青云交1 小时前
Java 大视界 -- Java 大数据机器学习模型在金融信用评级模型优化与信用风险动态管理中的应用(371)
java·大数据·机器学习·信用评级·动态风控·跨境金融·小贷风控
liosen2 小时前
【安卓笔记】OOM与内存优化
android·oom·内存优化·内存分析命令·内存分析工具
求知若渴,虚心若愚。3 小时前
Error reading config file (/home/ansible.cfg): ‘ACTION_WARNINGS(default) = True
linux·前端·ansible
LinDaiuuj4 小时前
最新的前端技术和趋势(2025)
前端
二哈喇子!4 小时前
若依【(前后端分离版)SpringBoot+Vue3】
java·spring boot·后端
paopaokaka_luck4 小时前
婚纱摄影管理系统(发送邮箱、腾讯地图API、物流API、webSocket实时聊天、协同过滤算法、Echarts图形化分析)
vue.js·spring boot·后端·websocket·算法·echarts