位于底部的提示View
支持侧滑消失
同一时间只有一个
不支持跨Activity展示
国内使用率很低
Snackbar.make(view, "Content", Snackbar.LENGTH_LONG).show()
注意不设置点击事件回调,点击按钮的文字不会展示出来
.setAction("click") {Log.d("TAG", "click")}
会在设置的 View 上方展示
.setAnchorView(R.id.flb_main)
如果不设置 AnchorView ,默认都是在底部展示的,那么有方法展示在中间么?
private static ViewGroup findSuitableParent(View view) {ViewGroup fallback = null;do {if (view instanceof CoordinatorLayout) {// We've found a CoordinatorLayout, use itreturn (ViewGroup) view;} else if (view instanceof FrameLayout) {if (view.getId() == android.R.id.content) {// If we've hit the decor content view, then we didn't find a CoL in the// hierarchy, so use it.return (ViewGroup) view;} else {// It's not the content view but we'll use it as our fallbackfallback = (ViewGroup) view;}}if (view != null) {// Else, we will loop and crawl up the view hierarchy and try to find a parentfinal ViewParent parent = view.getParent();view = parent instanceof View ? (View) parent : null;}} while (view != null);// If we reach here then we didn't find a CoL or a suitable content view so we'll fallbackreturn fallback;}
源码中会根据设置的view去找到最近的一个CoordinatorLayout,如果找不到就去找
FrameLayout 用来备用,直到找到顶层的 id 为 android.R.id.content 的 Fragment,
所以如果在布局中加入一个 CoordinatorLayout 完全可以让Snackbar展示在中间。
//下一个Snackbar出现,或者调用dismiss会消失public static final int LENGTH_INDEFINITE = -2;//短时间 1500 实际消失时间还会加上动画消失时间public static final int LENGTH_SHORT = -1;//长时间 2750 实际消失时间还会加上动画消失时间public static final int LENGTH_LONG = 0;//SnackbarManagerprivate static final int SHORT_DURATION_MS = 1500;private static final int LONG_DURATION_MS = 2750;
//背景颜色
.setBackgroundTint()
//点击按钮颜色
.setActionTextColor()
//文字颜色
.setTextColor()
.addCallback(object: BaseCallback() {})
内容最多2行,超过会省略
按钮文字没有限制行数,短字数的情况下和内容在一行展示,多了会显示在内容下方。
如果想click展示完全可以扩大显示宽度
.setMaxInlineActionWidth(100)
使用还是比较少的,常见于 Android 项目的 Demo