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();
    }
});
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();
        }
    });
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();
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>
image

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,593评论 25 709
  • 滑动菜单可以说是Material Desgin中最常见的效果之一了,在许多著名的应用中,都有滑动菜单的功能...
    AndYMJ阅读 2,611评论 2 4
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,031评论 3 119
  • 有一种幸福 是朋友 亲爱的老朋友 让落叶带去我的思念 你们在的地方 就是我最美的故乡 只是倦的时候 想起你们的笑容...
    矜舒阅读 121评论 0 0
  • 第一章:童年一 上一章:去江西五 本章: 去江西六 我在潭口林场的时间不长,因为总场决定要把所有分场、林场的公路...
    丹石阅读 1,342评论 22 15