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

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

相关推荐
幻雨様3 小时前
UE5多人MOBA+GAS 45、制作冲刺技能
android·ue5
在努力的前端小白3 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
Jerry说前后端4 小时前
Android 数据可视化开发:从技术选型到性能优化
android·信息可视化·性能优化
专注API从业者4 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
bobz9655 小时前
小语言模型是真正的未来
后端
烛阴5 小时前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
Meteors.5 小时前
Android约束布局(ConstraintLayout)常用属性
android
一叶飘零_sweeeet5 小时前
从繁琐到优雅:Java Lambda 表达式全解析与实战指南
java·lambda·java8
DevYK5 小时前
企业级 Agent 开发实战(一) LangGraph 快速入门
后端·llm·agent
alexhilton6 小时前
玩转Shader之学会如何变形画布
android·kotlin·android jetpack