首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Toast.getView()在Android11上返回null (API 30)

Toast.getView()在Android11上返回null (API 30)
EN

Stack Overflow用户
提问于 2020-07-13 21:08:29
回答 6查看 11.8K关注 0票数 33

我刚刚在我的模拟器中安装了Android (API 30)图像来尝试我的应用程序,当我试图设置一个Toast的背景色时,它就崩溃了。

代码语言:javascript
运行
复制
    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 (API 29)中工作非常完美。

我的build.gradle更新了安卓R (API 30)

代码语言:javascript
运行
复制
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

有什么新方法吗??

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2020-07-13 22:22:19

谷歌表示,自从Android 11之后,定制祝酒词/吐司修改就不再受欢迎了,以“保护用户”。这就是为什么你的Android 30应用程序不能显示自定义祝酒的原因。

来自Android开发人员的文档:

不建议使用

自定义吐司视图。应用程序可以用makeText(android.content.Context,java.lang.CharSequence,int)

创建标准文本吐司。

票数 25
EN

Stack Overflow用户

发布于 2020-10-12 17:19:06

我发现从API 30开始显示自定义祝酒词的唯一方法是临时创建它们。

XML布局

根据需要自定义

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".main_activity">



        <!--Ad hoc toast Textview-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:layout_margin="18dp"
            android:background="@drawable/ad_hoc_toast_background"
            android:textColor="#1e1e1e"
            android:gravity="center"
            android:visibility="gone"
            android:layout_alignParentBottom="true"
            android:id="@+id/ad_hoc_toast_textview"
            tools:text="Temporary message bla bla bla ..."/>



</RelativeLayout>

吐司背景 (ad_hoc_toast_background.xml)

根据需要自定义

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle">

        <size
                 android:width="220dp"
                 android:height="100dp"/>
        <corners
                 android:radius="25dp"
            />

        <solid
                 android:color="#e6ffffff"
            />
    </shape>
</item>
</selector>

show_ad_hoc_toast() 定义方法

代码语言:javascript
运行
复制
private void show_ad_hoc_toast(final TextView ad_hoc_toast_textview, String text){


    //Set the text
    ad_hoc_toast_textview.setText(text);


    //Create alpha animation
    AlphaAnimation animation1 = new AlphaAnimation(0f, 1f);

    //Set duration
    animation1.setDuration(300);

    //Set that the animation changes persist once the animation finishes
    animation1.setFillAfter(true);


    //Set on AnimationEnd Listner
    animation1.setAnimationListener(new Animation.AnimationListener() {

        @Override public void onAnimationStart(Animation animation){}
        @Override public void onAnimationRepeat(Animation animation){}
        @Override public void onAnimationEnd(Animation animation){

            //After 2250 millis -> hide the toast
            new CountDownTimer(2250, 1) {
                public void onTick(long millisUntilFinished){}
                public void onFinish() {hide_ad_hoc_toast(ad_hoc_toast_textview);}
            }.start();


        }

    });


    //Make the view visible
    ad_hoc_toast_textview.setVisibility(View.VISIBLE);


    //Start animation
    ad_hoc_toast_textview.startAnimation(animation1);


}

hide_ad_hoc_toast() 定义方法

代码语言:javascript
运行
复制
private void hide_ad_hoc_toast(final TextView ad_hoc_toast_textview){


    //Create alpha animation
    AlphaAnimation animation1 = new AlphaAnimation(1f, 0f);

    //Set duration
    animation1.setDuration(300);

    //Set that the animation changes persist once the animation finishes
    animation1.setFillAfter(true);


    //Set on AnimationEnd Listner
    animation1.setAnimationListener(new Animation.AnimationListener() {

        @Override public void onAnimationStart(Animation animation) { }
        @Override public void onAnimationRepeat(Animation animation) { }
        @Override public void onAnimationEnd(Animation animation) {

            //Make the view gone
            ad_hoc_toast_textview.setVisibility(View.GONE);

        }

    });



    //Start animation
    ad_hoc_toast_textview.startAnimation(animation1);



}

在需要时从代码中调用方法

代码语言:javascript
运行
复制
//Find ad_hoc_toast textview
TextView ad_hoc_toast_textview = findViewById(R.id.ad_hoc_toast_textview);

//Define the text to be shown
String text = "This is the custom toast message"

//Show the ad_hoc toast
show_ad_hoc_toast(ad_hoc_toast_textview, text);  

结果

票数 7
EN

Stack Overflow用户

发布于 2021-02-02 06:51:14

你可以在吃烤面包之前检查一下

代码语言:javascript
运行
复制
    Toast toast = Toast.makeText(ctxt, msg, duration);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        View view = toast.getView();
        view.setBackgroundColor(0xFF303030);
        TextView tview = view.findViewById(android.R.id.message);
        tview.setTextColor(Color.WHITE);
    }
    toast.show();
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62884286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档