如何在屏幕顶部显示Snackbar [英] How to show Snackbar at top of the screen

查看:938
本文介绍了如何在屏幕顶部显示Snackbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如Android文档所述

小吃栏提供有关操作的轻量级反馈.它们在移动设备的屏幕底部显示一条简短消息,在较大的设备上则显示在左下方.

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices.

是否可以通过其他方式将snackbars显示在屏幕顶部而不是底部?

Is there any alternative by which we can show snackbars at top of the screen instead of the bottom?

现在我正在执行类似的操作,在屏幕底部显示snackbar.

Right now I am doing something like this which shows a snackbar at the bottom of the screen.

Snackbar.make(findViewById(android.R.id.content), "Hello this is a snackbar!!!", 
Snackbar.LENGTH_LONG).setAction("Undo", mOnClickListener)
.setActionTextColor(Color.RED)
.show();

推荐答案

可以使用以下方法使小吃栏出现在屏幕顶部:

It is possible to make the snackbar appear on top of the screen using this:

Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();

在OP中:

我不得不更改第一行:

I had to change the first line:

Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG);

这篇关于如何在屏幕顶部显示Snackbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆