Snackbar详解

简介: Snackbar是什么Snackbar是Design Support Library库中的一个控件它是用来替代Toast的一个全新的控件,Snackbar与Toast最大的区别是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

目录
相关文章
|
Android开发
解决圆形进度条ProgressBar的几个问题
Android自带的Progressbar默认就是圆形的,可以通过设置style属性 style="?android:attr/progressBarStyleHorizontal" 复制代码 这样就能变成条状进度条,如下: <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"/>
1079 0
|
API Android开发 容器
Snackbar-Android M新控件
Snackbar-Android M新控件
75 0
|
API Android开发 容器
PopupWindow
PopupWindow
110 0
|
Android开发
SearchView使用详解
SearchView使用详解
365 0
SearchView使用详解
Dialog和DialogFragment 设置背景透明
Dialog和DialogFragment 设置背景透明
929 0
Snackbar使用详解
Snackbar使用详解
196 0
Snackbar使用详解