14

我正在开发一个项目,并通过执行以下操作将应用程序的背景设置为白色:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarSize">140dp</item>
    <item name="android:background">#ffffff</item>
</style>

这很有效,但问题是 toast 消息现在以白色背景显示。奇怪的是我在项目中集成了一个启动画面,当用户登录时,吐司消息正常显示。

这真的很奇怪,希望能在这个问题上提供任何帮助。

编辑:添加屏幕截图显示问题。屏幕截图是在最初的 toast(具有不需要的效果)淡出和新的 toast(默认)淡入时拍摄的。

在此处输入图像描述

4

5 回答 5

28

我解决了这个问题。Toast 背景颜色发生变化的原因是我在 View 对象的上下文中传递的方式,它包含在其中。

以下代码行将导致背景颜色更改为不需要的白色:

Toast.makeText(v.getContext(), "Checking login details...", Toast.LENGTH_SHORT).show();

这行代码会将 Toast 返回到默认系统样式:

Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show();

我不确定像这样修复它是否存在很大问题,因为我只是在学习。如果有人可以看到问题,请分享。它似乎工作得很好。

于 2013-01-09T19:34:22.760 回答
0

对我来说使用getApplicationContext()不是一个选项,对于其他有同样问题的人,您可以将 Toast 设置回默认设置,如下所示:

//Create your Toast with whatever params you need
Toast toast = Toast.makeText(getActivity(), "Refreshing...", Toast.LENGTH_SHORT);  

//Set the background for the toast using android's default toast_frame.
//Optionally you can set the background color to #646464 which is the
//color of the frame
View view = toast.getView();
view.setBackgroundResource(android.R.drawable.toast_frame); 

//Get the TextView for the toast message so you can customize
TextView toastMessage = (TextView) view.findViewById(android.R.id.message); 

//Set background color for the text.
toastMessage.setBackgroundColor((Color.parseColor("#646464"))); 
toast.show();
于 2015-05-15T19:30:50.823 回答
-1

WInthrop 答案的补充。除了将文本框的背景颜色设置为#646464,还可以将其设置为透明,使吐司看起来像原来的半透明吐司

private void showToast(Context context,String msg,int duration){
        Toast toast = Toast.makeText(context,msg,duration);

        View view = toast.getView();
        view.setBackgroundResource(android.R.drawable.toast_frame);

        TextView toastMessage = (TextView) view.findViewById(android.R.id.message);

        toastMessage.setBackgroundColor(Color.TRANSPARENT);

        toast.show();
    }
于 2016-07-04T16:00:19.570 回答
-1

这对我有用。我拿了Sachin Murali G 的密码

 private void showToast(Context context, String msg, int duration) {
        Toast toast = Toast.makeText(context, msg, duration);
        View view = toast.getView();
        view.setBackgroundResource(android.R.drawable.toast_frame);
        view.setBackgroundColor(Color.TRANSPARENT);
        TextView text = view.findViewById(android.R.id.message);
        text.setBackground(context.getResources().getDrawable(R.drawable.custom_toast));
        text.setTextColor(context.getResources().getColor(R.color.colorPrimaryLight));
        toast.show();
    }

custom_toast.xmldrawable文件夹中添加:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="22dp"/>
    <solid android:color="@color/colorPrimary"/>
    <padding
        android:bottom="12dp"
        android:left="20dp"
        android:right="20dp"
        android:top="12dp"/>
</shape>

非常感谢!

于 2019-02-15T14:56:54.040 回答
-2

试试这个:

toast.getView().setBackgroundColor(0xFF00ddff);
于 2019-05-04T10:56:52.220 回答