Android:将Toast设置为默认位置(居中,位于底部状态栏上方)?

发布于 2024-12-13 14:00:12 字数 742 浏览 2 评论 0原文

我遇到了与这篇帖子完全相反的问题 -具体来说,我想在默认位置(居中,位于底部状态栏上方)显示一个吐司,但它总是水平和垂直居中显示。

这是我用来显示 toast 的代码和调用(toastNavigation 方法位于与调用不同的类中):

public static void toastNavigation(Context context, CharSequence message,
        int duration, int gravity, int gravity_xOffset, int gravity_yOffset) {  
    Toast toast = Toast.makeText(context, message, duration);  
    toast.setGravity(gravity, gravity_xOffset, gravity_yOffset);  
    toast.show();
}

toastNavigation(this, 
    "My message", Toast.LENGTH_SHORT, Gravity.NO_GRAVITY, 0, 0);

为什么我的 toast 会居中,即使我传入了指示“. ..没有设置重力。”?我是否应该传递一些其他常量来清除从上下文继承的重力常量?

I'm having the exact opposite problem of this post - specifically, I want to display a toast in the default location (centered, just above the status bar at the bottom) but it is always appearing horizontally and vertically centered.

Here is the code and call I am using to display the toast (toastNavigation method is in a separate class from call):

public static void toastNavigation(Context context, CharSequence message,
        int duration, int gravity, int gravity_xOffset, int gravity_yOffset) {  
    Toast toast = Toast.makeText(context, message, duration);  
    toast.setGravity(gravity, gravity_xOffset, gravity_yOffset);  
    toast.show();
}

toastNavigation(this, 
    "My message", Toast.LENGTH_SHORT, Gravity.NO_GRAVITY, 0, 0);

Why would my toast be centered even though I am passing in the constant that indicates "...no gravity has been set."? Is there some other constant I should pass to clear GRAVITY constants inherited from the context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻之岛 2024-12-20 14:00:13

我检查了您的代码以在屏幕上显示吐司。
做一件事,请删除对 setGravity 方法的调用,然后您的 toast 将根据您的要求出现在确切的位置。
如果您尝试将 NO_GRAVITY 设置为 Toast 的重力,那么在这种情况下,Toast 将选择默认重力,并且 Toast 的默认重力值在窗口中居中(与对话框相同)。
如有任何疑问,请告诉我。
谢谢。

I checked your code to display toast on screen.
Do one thing, please remove call to setGravity method, then your toast will appear on exact location, as per your requirement.
If you try to set NO_GRAVITY as gravity of your toast, then in this case toast will pick default gravity, and value of default gravity for toast is Centered in window(same as for dialog).
Please let me know for any concern.
Thanks.

自此以后,行同陌路 2024-12-20 14:00:13

如果您想在默认位置显示 Toast,只需这样调用即可:

    Toast.makeText(context, message, duration).show();

这将在标准位置显示 Toast。我的猜测是 NO_GRAVITY 不是 Toast 的默认设置。它可能使用重力底部并应用 y 值将其从屏幕底部拉起。

If you would like to display a Toast in teh default location just call it like so:

    Toast.makeText(context, message, duration).show();

This will display the toast in the standard location. My guess is the NO_GRAVITY is not the default setting for a Toast. It probably uses gravity bottom and applies a y value to bring it up from the bottom of the screen.

复古式 2024-12-20 14:00:12

如果您只需要默认值,我假设您有充分的理由调用 setGravity()

Toast 的默认重力设置为 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM,因此您可以应用它们来获得默认位置。

您可以在Toast 源...检查 mGravity 字段的初始化。

I'm going to assume you have good reason for calling setGravity() at all if you just need the default values.

The default gravity settings for a Toast are Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, so you can apply those to get the default placement.

You can see this in the source for Toast...check the initialization of the mGravity field.

秋凉 2024-12-20 14:00:12

对我来说,没有重力设置的 Toast 的默认位置是从底部向上 1/3 到 1/4 之间。

我发现 Gravity.BOTTOM(经常被引用为默认值)实际上将重力设置在屏幕的最底部。我还发现 Gravity.NO_GRAVITY 将 toast 设置在屏幕中央,就像 Gravity.CENTER 一样,而不是实际的默认位置。这是相当令人恼火的。我还没有找到一个设置可以将吐司设置回其真正的默认位置,我只需要制作一个新的吐司。

For me the default location of a toast without a gravity setting is between 1/3 and 1/4 up from the bottom.

I've found Gravity.BOTTOM, which is often cited as the default, actually sets the gravity to the very bottom of the screen. Also I've found Gravity.NO_GRAVITY sets the toast in the center of the screen, just like Gravity.CENTER, instead of the actual default location. This is rather irritating. I've yet to find a setting that sets a toast back to it's true default location, I've just had to make a new toast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文