gpt4 book ai didi

android - Google Inbox 如何显示覆盖键盘的 snackbar ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:13 26 4
gpt4 key购买 nike

Google Inbox 如何在键盘上方(覆盖、分层)显示快捷栏?这不是默认行为,我已经用基本的协调器布局、键盘打开和 snackbar 进行了测试:默认行为是在键盘后面显示 snackbar 。这个问题有很多答案:“我如何在键盘上方显示 snackbar ”,但我还没有找到如何重现 Google 自己的收件箱应用程序的 snackbar 行为。

你能告诉我如何实现吗?

最佳答案

Inbox 不使用标准的 Snackbar 也不使用覆盖窗口,它使用自定义 View 。我反编译了 APK 以验证它们是如何实现这种效果的,并尝试重现一个最小的示例。

首先创建一个新布局custom_snackbar.xml:

<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);
}
}
});
}

这是获得的结果:

Screenshot

关于android - Google Inbox 如何显示覆盖键盘的 snackbar ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45760678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com