/**
* SnackBar辅助类
*
*/
public class SnackBarHelper {
public static void showLong(View anchorView, String res) {
showLongWithColor(anchorView, res, ApplicationController.getInstance().getResources().getColor(R.color.red));
}
public static void showLong(Activity context, String res) {
showLongWithColor(context.getWindow().getDecorView().findViewById(android.R.id.content), res, ApplicationController.getInstance().getResources().getColor(R.color.red));
}
/**
* 显示自定义背景色的SnackBar
*
* @param anchorView
* @param res
* @param color
*/
public static void showLongWithColor(View anchorView, String res, int color) {
Snackbar snackbar = Snackbar.make(anchorView, res, Snackbar.LENGTH_LONG).setAction("Action", null);
View background = snackbar.getView();
background.setBackgroundColor(color);//设置为主色调
snackbar.setDuration(1500);
snackbar.show();
}
}
在Activity中调用
//第一种
SnackBarHelper.showLong(getWindow().getDecorView().findViewById(android.R.id.content), "这是一条提示");
//第二种
SnackBarHelper.showLong(this, "这是一条提示");