/* if you use STYLE_SHOW_TOP and your activity has toolbar or actionbar ,you should use "findViewById(android.R.id.content)",must not use "getWindow().getDecorView()"*/ TBSnackbar.make(findViewById(android.R.id.content), "This is a top snack!", TBSnackbar.LENGTH_SHORT, TBSnackbar.STYLE_SHOW_TOP).show();
2. 从底部弹出:
要想从底部弹出,要使用TBSnackbar.STYLE_SHOW_BOTTOM模式,具体代码如下:
1 2 3
// if you use STYLE_SHOW_BOTTOM ,you can use any view.But if you use //CoordinatorLayout,you must use CoordinatorLayout. TBSnackbar.make(findViewById(android.R.id.content), "This is a bottom snack!", TBSnackbar.LENGTH_SHORT,TBSnackbar.STYLE_SHOW_BOTTOM).show();
// if you use STYLE_SHOW_TOP_FITSYSTEMWINDOW ,you must use //getWindow().getDecorView() android api >= 19 TBSnackbar.make(getWindow().getDecorView(),"This is a fitsystemwindow snack!", TBSnackbar.LENGTH_SHORT,TBSnackbar.STYLE_SHOW_TOP_FITSYSTEMWINDOW).show();
4. 添加图标:
1 2 3 4 5 6
//setIconLeft(@DrawableRes int drawableRes, float sizeDp) the size is dp,24dp is ok //if you want change the icon padding you can use setIconPadding(int padding) //setIconRight(@DrawableRes int drawableRes, float sizeDp) you can use TBSnackbar snackbar = TBSnackbar.make(findViewById(android.R.id.content), "This is a left icon snack!", TBSnackbar.LENGTH_SHORT, TBSnackbar.STYLE_SHOW_TOP); snackbar.setIconLeft(R.mipmap.ic_core,24); snackbar.show()
5. 添加action:
1 2 3 4 5 6 7 8 9
//you can use it like google's SnackBar final TBSnackbar snackbar = TBSnackbar.make(findViewById(android.R.id.content), "This is a action snack!", TBSnackbar.LENGTH_INDEFINITE, TBSnackbar.STYLE_SHOW_TOP); snackbar.setAction("Action", new View.OnClickListener() { @Override publicvoidonClick(View v){ snackbar.dismiss(); } }); snackbar.show();