SnackBar介绍

SnackBar是Android Support Design Library库中的一个控件,可以在屏幕底部弹出消息,比Toast更加突出。

以下是SnackBar源代码对SnackBar的一个介绍。

/**
 * Snackbars provide lightweight feedback about an operation. They show a brief message at the
 * bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other
 * elements on screen and only one can be displayed at a time.
 * 

* They automatically disappear after a timeout or after user interaction elsewhere on the screen, * particularly after interactions that summon a new surface or activity. Snackbars can be swiped * off screen. *

* Snackbars can contain an action which is set via * {@link #setAction(CharSequence, android.view.View.OnClickListener)}. *

* To be notified when a snackbar has been shown or dismissed, you can provide a {@link Callback} * via {@link #setCallback(Callback)}.

*/
要使用SnackBar必须导入下列包

import android.support.design.widget.Snackbar;

下面的代码是我编写的一个非常简单的Demo

package com.kavinapps.androidk.mysnackbar;

import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Snackbar snackbar;
    CoordinatorLayout coordinatorLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coor);

        snackbar.make(coordinatorLayout,"这是message",Snackbar.LENGTH_LONG)
                .setAction("This is action", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"You click the action",Toast.LENGTH_SHORT).show();
            }
        }).show();
    }
}


也就是说,实例化SnackBar之后,直接使用make()方法就可以。

这里简单说一下make()中的第一个参数coordinatorLayout,其实我的layout下的activity_main.xml非常简单,如下代码所示。




    
    
    
之所以使用coordinatorLayout,是可以确保Snackar和其它组件的交互,比如滑动取消SnackBar、SnackBar出现时FloatingActionButton上移


等等

你可能感兴趣的:(android,基础)