简体   繁体   English

在 Android 中居中 Toast 消息

[英]Centering Toast Message in Android

Ok, I feel like I've tried every configuration possible, but I can't seem to position my Toast message in the center of the screen of my Android application.好的,我觉得我已经尝试了所有可能的配置,但我似乎无法在 Android 应用程序的屏幕中央显示我的 Toast 消息。 It keeps showing up in the default position, near the bottom of the screen.它一直显示在屏幕底部附近的默认 position 中。

This is the code I'm currently using but, again I've tried a lot of different configurations.这是我目前正在使用的代码,但是我再次尝试了很多不同的配置。 Any ideas?有任何想法吗?

   Toast toast = Toast.makeText(this,"Please Set a Profile", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
   toast.show();

The setGravity works on all versions below R . setGravity适用于R以下的所有版本。 It has been discontinued from the R .它已从R中停产。 Check this in the documentation.在文档中检查这一点。

https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int) https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int)

Warning: Starting from Android Build.VERSION_CODES#R, for apps targeting API level Build.VERSION_CODES#R or higher, this method is a no-op when called on text toasts.警告:从 Android Build.VERSION_CODES#R 开始,对于面向 API 级别 Build.VERSION_CODES#R 或更高级别的应用程序,此方法在文本 toasts 上调用时是无操作的。

So, either you target a lower version or you can find other options like creating a custom view that behaves like a toast.因此,您要么针对较低版本,要么可以找到其他选项,例如创建行为类似于 toast 的自定义视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM