Snackbar详解

Snackbar是什么

Snackbar是Design Support Library库中的一个控件它是用来替代Toast的一个全新的控件,Snackbar与Toast最大的区别是Snackbar支持点击和滑动和滑动消失,如果用户没有进行操作它也会在到达指定时间后自动消失。

Snackbar的使用

●最简单的Snackbar

button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       Snackbar.make(view,"我是Snackbar",Snackbar.LENGTH_SHORT).show();
    }
});
img_1770a3fff6a8b1aef27b25f6714effff.gif
image

●能够点击的Snackbar

button=findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view,"我是Snackbar",Snackbar.LENGTH_SHORT).setAction("点击", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, "我是点击后的信息", Toast.LENGTH_SHORT).show();
                }
            }).show();
        }
    });
img_7b346eb152ccfe6d1c665f41f2e01f20.gif
image

●添加自定义布局

xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="点击"
        android:id="@+id/bttest"
        android:layout_height="wrap_content" />
</RelativeLayout>
java代码
Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_SHORT);
View view1 = snackbar.getView();
view1.setBackgroundColor(Color.RED);
Snackbar.SnackbarLayout snackbarLayout= (Snackbar.SnackbarLayout) view1;
View inflate = View.inflate(MainActivity.this, R.layout.snackbar, null);
inflate.findViewById(R.id.bttest).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
      Toast.makeText(MainActivity.this, "点击按钮啦", Toast.LENGTH_SHORT).show();
                }
});
snackbarLayout.addView(inflate);
snackbar.show();
img_e592d5a1a0bfc904ee11edc3db8c399c.gif
image

●将布局改为CoordinatorLayout可以实现滑动消失效果

xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
    android:id="@+id/content"
    tools:context=".MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:text="测试"
        android:id="@+id/button"
        android:layout_height="wrap_content" />


</android.support.design.widget.CoordinatorLayout>
img_5297565485a91cf13455e7f7cfb88dc3.gif
image

个人博客:https://myml666.github.io

来源:https://yq.aliyun.com/articles/665567


智能推荐

Android开发2(Toast,AlertDialog,Snackbar,TextInputLayout的使用)

阅读目录 前言 基础知识 拓展知识 实验内容 实验步骤 对话框总结 源码下载 注   回到顶部 前言   啦啦啦~又要和大家一起学习Android开发啦,博主心里好激动哒~   在上篇博文中,我们通过线性布局和基础组件的使用,完成了一个简单的学生课外体育积分电子认证系统的界面,本篇博文,将和大家一起熟悉Button、RadioButton、EditText等基本控件,探讨能够处理这些控件...

Android使用Snackbar类型转换错误引起的思考

简介         在之前的项目中使用了自定义Snackbar,今天在新项目中使用就出现点击崩溃的问题。查看日志报错堆栈信息。发现java.lang.ClassCastException(类转换异常),怎么会出现这个错误呢? 报错堆栈信息 查找原因 错误:LinearLayout cannot be cast to ...

Flutter学习笔记(20)--FloatingActionButton、PopupMenuButton、SimpleDialog、AlertDialog、SnackBar...

如需转载,请注明出处:Flutter学习笔记(20)--FloatingActionButton、PopupMenuButton、SimpleDialog、AlertDialog、SnackBar FloatingActionButton FloatingActionButton对应一个圆形图标按钮,悬停在内容之上,以展示对应程序中的主要动作,所以非常醒目,类似于iOS系统里的小白点按钮。 Flo...

Snackbar和Toast的花式使用,系统Toast的存在的问题

系统toast存在的问题: https://mp.weixin.qq.com/s/HQoAa5h-u8q32iWhs7dz_w   Foreword   这一篇是建立在我赌5毛你没见过这样的SpannableString基础上的,所以不会使用SpanUtils的小伙伴快去看看吧。 这一篇我会写得详尽一些,毕竟是为了一劳永逸,让大家也尽可能了解我是怎么做到的,这里,我会用图文结合...

「解放双手」老舅教你VS Code Disco

微信搜索【前端食堂】你的前端食堂,记得按时吃饭。 本文已收录在前端食堂 Github https://github.com/Geekhyt/front-end-canteen,感谢Star。 这是最好的时代,也是最坏的时代。 今年听到过最浪漫的一句话:我们在键盘上留下的余温,也将随时代传递到更远的将来。 感觉让理性的技术人多了份柔光滤镜。 也许你收藏了千篇万篇VS Code快捷键,很可惜却没能记住...

猜你喜欢

Linux(3):Shell基础

Linux(1):简介与系统安装:https://mp.csdn.net/postedit/88633915 Linux(2):常用命令:https://mp.csdn.net/postedit/88639718 鸟哥的Linux私房菜:https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/86.html 目录 9 Shell基...

C# wpf grid面板应用(4)

1新建一个项目,如下图所示 分析代码;Grid.ColumnDefinitions可以定义列宽 Grid.RowDefinitioons可以定义行高 Grid.column=“0” 代表第一列。 grid.Row=“1” 代表第二行。 Grid.columnspan=“2” 表示向右合并2个列 Grid.RowSpan=&ld...

用C# Socket解析Http1.0遇到的坑

前几天的天气糟糕透了,一连下了几天雨,昨天才刚刚放晴,俗话说天晴了,雨停了,我又感觉我行了,于是想给RRQMSocket添加新功能了。第一个想到的就是WebApi功能,那么解析Http就是首要工作,不过幸好网上资料丰富,而且Http协议也不难,不像mqtt一样逐bit分析。说干就干,一会就写了个解析器,于是测试。 首先使用的测试工具是Postman,效果还不错,最起码实现了功能,但是延迟需要3-5...

eclipse当git上传文件时存在冲突的处理方法

我在使用eclipse上传文件时,出现了上传文件冲突情况。 造成的原因:多个人对同一文件进行操作,部分人对某些代码进行删除,而本地未删除。当本地上传代码时,服务器比对产生错误。 解决方案:需要将最新的版本下载下来,然后对于最新版本进行修改后再进行提交。 具体措施: 首先找到出现冲突的文件:projectIndex.jsp 然后进行复制按ctrl+C,再粘贴在相同处ctrl+V 之后会出现两个文件内...

最全Pycharm教程(2)——代码风格

转载于 https://blog.csdn.net/u013088062/article/details/50113593 1、主题   这部分教程主要介绍如何创建一个Python工程并使其具有Pycharm的代码风格。你将会看到Pycharm使你的源码变得非常简洁美观,带有合适的缩进、空格等等,因此Pycharm也是一款代码质量管理的利器。   这部分教程并不会介绍如何使用Python...

问答精选

typescript - conditional paramteres

I want to restrict one of the constructor parameter of number type to assign only the values from some range. I know how to archieve similiar effect with setter and conditional statement. I was wonder...

My CSV writer code writes separators between characters and not strings

I have written code which writes to a CSV file, reading from another file. I want to write out particular columns from the input file, so I append them to a list, then separate them by a comma and add...

How to open a Xamarin Forms page from a Xamarin Droid Activity from a onclick listener

I am working on adding an assist feature to my app that when the user holds down on the home button an android activity opens with cards that the user can click on that then opens the xamarin forms pa...

Validation User Mysql Java?

I did this query with this development but I have a problem to receive the API I try to validate Users Login But always the API RESPONSE LOGIN SUCCESS I did this with Java Spring Boot and Mysql Here's...

How convert to array or json php soap __dorequest response?

this is _dorequest response. Response is xml string data. I tried convert with simplexml_load_string() function and later return array but function result is empty. '<env:Envelope xmlns:env="h...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答