Android设计支持库

2023-09-22 00:22:56

本文所有的代码均存于
https://github.com/MADMAX110/BitsandPizzas

设计支持库(Design Support Library)是 Google 在 2015 年的 I/O 大会上发布的全新 Material Design 支持库,在这个 support 库里面主要包含了 8 个新的 Material Design 组件,最低支持 Android 2.1。
设计支持库提供了一系列 Material Design 风格的组件,帮助开发者快速构建符合 Material Design 规范的 Android 应用程序。这些组件包括:
TextInputLayout:EditText 辅助控件,提供输入提示、错误提示等功能。
FloatingActionButton:MD 风格的圆形按钮,通常用于表示主要操作。
Snackbar:提示框,用于显示简短的提示信息。
TabLayout:选项卡,用于在多个选项之间进行切换。
NavigationView:DrawerLayout 的侧滑界面,用于显示导航菜单。
CoordinatorLayout:超级 FrameLayout,用于实现复杂的布局交互效果。
AppBarLayout:MD 风格的滑动 Layout,用于实现可折叠的标题栏。
CollapsingToolbarLayout:可折叠的 MD 风格 ToolbarLayout,用于实现可折叠的标题栏。
MaterialButton:Material 风格的 Button。
Chip & ChipGroup:关键字标签组件。
MaterialCardView:Material 风格的 CardView。
BottomAppBar:底部标题栏。

针对之前的BitsandPizzas应用,我们可以为其添加更多的设计支持库的特性。
1、支持MainActivity的工具条滚动
2、为OrderActivity增加一个折叠工具条
3、向OrderActivity增加一个FAB浮动动作按钮
4、让FAB显示一个snackbar

一、让工具条响应滚动

1、修改MainActivity的布局以支持工具条滚动。
2、修改TopFragment,让它包含可滚动的内容。
与此同时,片段中的内容滚动时,我们要让工具条也滚动,为此要为MainActivity增加一个协调器布局CoordinatorLayout。协调器布局就像一个马力很足的帧布局,用来协调不同视图之间的动画和过渡。在这里我们将用协调器布局协调TopFragment中的可滚动内容和MainActivity的工具条。

下面是在MainActivity的布局增加一个协调器布局。
除此之外还要协调滚动行为。
标记用户将滚动的视图,让工具条对它做出响应。
要标记用户将滚动的视图,需要指定该视图的属性app:layout_behavior,把它设置为内置字符串“@string/appbar_scrolling_view_behavior”。
这会告诉协调器布局:你希望用户滚动这个视图时应用条布局中的视图能够做出响应。在这里我们希望工具条滚动来相应用户滚动视图分页控件的内容,所以要为ViewPager元素增加app:layout_behavior属性。

还需要告知工具条响应滚动事件:使用属性app:layout_scrollFlags告诉应用条布局中的视图如何响应滚动事件。
在这里设置为当用户向上滚动视图分页控件内容时,工具条要向上滚动移出屏幕,当用户向下滚动时,它要迅速返回原来的位置。为此要把Toolbar的该属性设置为scroll | enterAlways、Scroll使得视图可以滚动移出屏幕,不然将会固定在屏幕上方、enterAlways表示用户滚动相应的视图时工具条也会快速向下滚动到它原来的位置。即使没有这个值,工具条也会向下滚动,不过会比较慢。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

二、为TopFragment增加可滚动的内容

修改这个片段使其包含一个图像和一些文本可使其滚动。
1、增加字符串资源

<string name="company_name">Bits and Pizzas</string>
<string name="restaurant_image">Restaurant image</string>
<string name="home_text">Since we opened our doors in 2017, Bits and Pizzas
        has built its reputation as one of America’s best Italian-Digital restaurants. Some
        people believe eating out is about being with your friends and family. We believe that
        good food is best enjoyed while staring at your phone.</string>

2、向drawable文件夹中增加下面的图片
在这里插入图片描述

三、使用嵌套滚动视图使布局内容可滚动

使用一个嵌套滚动视图,允许用户滚动TopFragment的内容。这种视图的工作与普通的滚动视图很类似,只不过它还支持嵌套滚动,因为协调器布局只监听嵌套滚动事件。如果在布局中使用一个普通的滚动视图,用户滚动内容时工具条并不会滚动。
现在TopFragment的布局结构要增加一个餐厅图像和一些文本。在写代码之前先来分析一下这个布局的结构。
1、整个片段都是可以滚动的,这说明我们要把所有的视图都放在一个嵌套滚动视图中。
2、我们要使用两个文本视图显示公司名和文本。这两个文本视图将放在一个有白色背景的垂直线性布局中。
3、我们希望在一个图像上面显示这个包含两个文本视图的线性布局。为此要把它们放在一个帧布局中。
下面是fragment_top.xml的完整代码。

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TopFragment">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/info_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/img"
            android:contentDescription="@string/restaurant_image"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:padding="16dp"
            android:background="#FFFFFF"
            android:orientation="vertical" >

            <TextView
                android:textSize="32sp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/company_name" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/home_text"/>

        </LinearLayout>
    </FrameLayout>

</androidx.core.widget.NestedScrollView>

试一试应用
向上滚动内容时,工具条也向上滚动。
在这里插入图片描述

四、向OrderActivity增加折叠工具条

除了让工具条滚动,另一种做法是折叠工具条。折叠工具条开始时很大,用户向上滚动屏幕内容时,折叠工具条会收缩,而当用户向下滚动屏幕内容时,折叠工具条会再展开。甚至可以为它增加一个图像,当工具条达到它的最小高度时,这个图像会消失,而当工具条展开时图像会重新可见。
首先增加一些字符串资源:

    <string name="order_name_hint">Please enter your name</string>
    <string name="order_details_hint">Please enter your order</string>

更新activity_order.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OrderActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/img"
                android:contentDescription="@string/restaurant_image"
                app:layout_collapseMode="parallax"/>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

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

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/order_name_hint" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/order_name_hint" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

试一试应用:
在这里插入图片描述

向上滑动后变为:
在这里插入图片描述

五、FAB和snackbar

FAB是浮动动作按钮,这是一个圆形图标,浮在用户界面之上,例如可能位于屏幕的右下角。FAB用来提示一些相当常用或者非常重要的动作,用户能够一目了然地看到它。
snackbar和toast很类似,不过snackbar还允许交互。这是一个简短的消息,显示在屏幕的下方,可以为用户提供某个操作有关信息。与toast不同,可以为snackbar增加动作,如撤销某个操作的动作。
增加FAB图标,将下列图标增加到工程中,命名为done
在这里插入图片描述
向activity_order.xml布局中增加FAB

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:src="@drawable/done"
        android:onClick="onClickDone" />

让用户单击FAB时显示一个snackbar
更新OrderActivity.java代码:

package com.hfad.bitsandpizzas;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;

public class OrderActivity extends AppCompatActivity {

    @SuppressLint("RestrictedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //需要使用支持库中的工具条
        ActionBar actionBar = getSupportActionBar();
        //启用向上按钮,尽管我们要用一个工具条作为应用条
        actionBar.setDefaultDisplayHomeAsUpEnabled(true);
    }

    public void onClickDone(View view){
        CharSequence text = "Your order has been updated";
        //让其显示很短的时间
        int duration = Snackbar.LENGTH_SHORT;
        Snackbar snackbar = Snackbar.make(findViewById(R.id.coordinator), text, duration);
        snackbar.setAction("Undo", new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(OrderActivity.this, "Undone!", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        snackbar.show();
    }
}

试一试

android:id="@+id/coordinator"

更多推荐

有多条业务线,mysql建多库多表比较好还是一个库多个表比较好呢?

这个问题的答案取决于您的具体需求。以下是一些需要考虑的因素:数据独立性:如果您的业务线之间的数据是独立的,并且不太可能需要进行跨业务线的查询,那么将它们分成多个数据库可能是有意义的。这样可以使每个业务线的数据更加独立,减少潜在的冲突和竞态条件。查询性能:在某些情况下,将数据分为多个表可能会提高查询性能。例如,如果您的数

Qt(day5)

思维导图将登录操作和数据库绑定mywnd.h#ifndefMYWND_H#defineMYWND_H#include<QMainWindow>#include<QLabel>#include<QLineEdit>#include<QPushButton>#include<QDebug>#include<QMessage

程序人生,中秋共享

程序人生,中秋共享随着科技的迅速发展,中秋节也悄然发生了变化。在这个传统的团圆节日里,程序人生与中秋共享形成了新的气象。在本文中,我将结合自己的职业经历,探讨程序人生与中秋共享之间的联系与意义。【中秋与程序人生的交融】作为程序员,我们每天都在和代码打交道,通过一行行指令来构建世界。然而,在这个机械化的过程中,我们也不忘

LLM预训练之RLHF(一):RLHF及其变种

在ChatGPT引领的大型语言模型时代,国内外的大模型呈现爆发式发展,尤其是以年初的LLaMA模型为首的开源大模型和最近百川智能的baichuan模型,但无一例外,都使用了「基于人类反馈的强化学习」(RLHF)来提升语言模型的性能,并在模型重注入了人类的偏好,以提高模型的有用性和安全性。不过RLHF也早已更新换代,我们

【Spring Boot】操作Redis数据结构

🌿欢迎来到@衍生星球的CSDN博文🌿🍁本文主要学习【SpringBoot】操作Redis数据结构🍁🌱我是衍生星球,一个从事集成开发的打工人🌱⭐️喜欢的朋友可以关注一下🫰🫰🫰,下次更新不迷路⭐️💠作为一名热衷于分享知识的程序员,我乐于在CSDN上与广大开发者交流学习。💠我希望通过每一次学习,让更多读

【MySql】1- 基础篇(上)

文章目录1.1前言1.2基础架构1.2.1MySql基本架构示意图1.2.2SQL语句执行顺序1.3日志系统:一条SQL更新语句如何执行1.3.1两阶段提交1.4事务隔离1.4.1隔离性与隔离级别1.4.2事务隔离的实现-展开说明“可重复读”1.4.3事务的启动方式1.5深入浅出索引1.5.1索引常见模型1.5.1In

双周赛113(枚举、分类讨论 + 二分查找、枚举值域两数之和、换根DP)

文章目录双周赛113[2855.使数组成为递增数组的最少右移次数](https://leetcode.cn/problems/minimum-right-shifts-to-sort-the-array/)暴力枚举贪心O(n)[2856.删除数对后的最小数组长度](https://leetcode.cn/problem

91. 面试官:JSONP的原理是什么?

91期1.JSONP的原理是什么?2.css优先级是什么样的?3.display的值有哪些,分别有什么作用?上面问题的答案会在第二天的公众号(程序员每日三问)推文中公布90期问题及答案1.什么是前后端分离,好处是什么?前后端分离是一种软件架构模式,它将前端和后端开发分离为两个独立的工作流程和技术栈,它们通过API或We

Java 中 jps 命令

jps(JavaVirtualMachineProcessStatusTool)是Java开发工具包(JDK)中的一个命令行工具,用于查看Java虚拟机(JVM)中运行的Java进程的状态信息。它通常用于检查正在运行的Java应用程序的进程ID(PID)和相关信息,这对于调试和性能监控非常有用。以下是jps命令的使用示

LeetCode 之 长度最小的子数组

算法模拟:AlgorithmVisualizer在线工具:C++在线工具如果习惯性使用VisualStudioCode进行编译运行,需要C++11特性的支持,可参考博客:VisualStudioCode支持C++11插件配置长度最小的子数组LeetCode长度最小的子数组问题:给定一个含有n个正整数的数组和一个正整数t

CAN总线物理层

本文的目的并不是为了介绍或普及CAN总线相关知识,而是为了了解CAN总线,进而为CAN通信一致性测试做知识储备。CAN,控制器局域网,全称:ControllerAreaNetwork。1986年,由德国Bosch公司为汽车开发的网络技术,主要用于汽车的监测与控制,目的为适应汽车“减少线束的数量”、“通过多个网络进行大量

热文推荐