我刚刚在我的模拟器中安装了 Android R (API 30) 图像来尝试我的应用程序,但在尝试设置 Toast 的背景颜色时它崩溃了。
Toast toast = Toast.makeText(ctxt, msg, duration);
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
toast.show();
这真的很奇怪,因为在 Android Q (API 29) 中完美运行。我的 build.gradle 已针对 Android R (API 30) 更新
compileSdkVersion 30
buildToolsVersion "30.0.1"
有新的方法吗??
最佳答案
据谷歌称,自 Android 11 以来,自定义 toast/toast 修改已被弃用,以“保护用户”。因此,为什么您在 Android 30 中的应用无法显示自定义 toast。
来自 Android 开发者文档:
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
关于android - Toast.getView() 在 Android 11 (API 30) 上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62884286/