android - Toast.getView() 在 Android 11 (API 30) 上返回 null

标签 android toast

我刚刚在我的模拟器中安装了 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/

相关文章:

Android Toast.makeText(this, ....) 不适用于 AppCompatActivity

带有图标的 Android Toast

java - 如何在android中为Toast自定义背景、背景颜色和文字颜色

android - 将对象列表传递给 fragment

windows-8 - 我可以将图像作为数据 URI 嵌入到 Windows 8/10 Toast 通知中吗?

android - 为什么 LogCat 不显示完整消息?

android - 在 Android 上使用 KSOAP。自定义命名空间的问题

android - 从操作栏执行搜索

android - 如何从堆栈中删除最后一个 Activity ?

java - 这是在Android中使用Sqlite数据库的正确方法吗?