谷歌收件箱是如何在键盘上方显示 snackbar 的?这不是默认行为,我已经使用基本协调员布局、打开键盘和一个 snackbar 进行了测试:默认行为是在键盘后面显示 snackbar。网上有很多关于“如何在键盘上方显示 snackbar”的问题的答案,但我没找到如何复制谷歌自己收件箱应用程序的 snackbar 行为。
你能告诉我如何实现这个功能吗?
谷歌收件箱是如何在键盘上方显示 snackbar 的?这不是默认行为,我已经使用基本协调员布局、打开键盘和一个 snackbar 进行了测试:默认行为是在键盘后面显示 snackbar。网上有很多关于“如何在键盘上方显示 snackbar”的问题的答案,但我没找到如何复制谷歌自己收件箱应用程序的 snackbar 行为。
你能告诉我如何实现这个功能吗?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#323232"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Marked done"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:minWidth="48dp"
android:text="undo"
android:textAllCaps="true"
android:textColor="#a1c2fa"/>
</LinearLayout>
接下来创建这些方法以显示和取消自定义 Snackbar:
private View customSnackbar;
private void showCustomSnackbar(final Context context) {
customSnackbar = LayoutInflater.from(context).inflate(R.layout.custom_snackbar, null, false);
customSnackbar.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//action implementation
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.format = PixelFormat.TRANSLUCENT;
lp.gravity = Gravity.BOTTOM;
lp.flags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
customSnackbar.setLayoutParams(lp);
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
if (windowManager != null) {
windowManager.addView(customSnackbar, customSnackbar.getLayoutParams());
Point m = new Point();
windowManager.getDefaultDisplay().getSize(m);
int childMeasureSpecWidth = ViewGroup.getChildMeasureSpec(View.MeasureSpec.makeMeasureSpec(m.x, View.MeasureSpec.EXACTLY), 0, lp.width);
int childMeasureSpecHeight = ViewGroup.getChildMeasureSpec(View.MeasureSpec.makeMeasureSpec(m.y, View.MeasureSpec.EXACTLY), 0, lp.height);
customSnackbar.measure(childMeasureSpecWidth, childMeasureSpecHeight);
customSnackbar.setTranslationY(customSnackbar.getMeasuredHeight());
customSnackbar.animate()
.setDuration(300)
.translationX(0.0f)
.translationY(0.0f);
}
}
private void dismissCustomSnackbar(final Context context) {
customSnackbar.animate()
.setDuration(300)
.translationX(0.0f)
.translationY(customSnackbar.getMeasuredHeight())
.withEndAction(new Runnable() {
@Override
public void run() {
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
if (windowManager != null) {
windowManager.removeView(customSnackbar);
}
}
});
}
WindowManager
...但是为什么它能出现在IME上方?我以为Android 8.0中关于WindowManager
添加窗口的更改应该可以防止这种情况发生。无论如何,干得好! - CommonsWareTYPE_APPLICATION_OVERLAY
类型。在此示例中,视图具有默认类型 TYPE_APPLICATION
。 - Mattia Maestrini
TYPE_SYSTEM_OVERLAY
,在8.0+上是TYPE_APPLICATION_OVERLAY
)。我不使用Google Inbox,所以无法测试这种情况。但是,如果Inbox在8.0+上没有这种行为,但在7.1及更早版本上有,则很可能是一个覆盖层,因为在8.0+上,覆盖层无法覆盖IME。 - CommonsWare