提示框背景颜色被改变

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(有不必要的效果)淡出并新的 toast(默认的)淡入时拍摄的。

enter image description here

5个回答

27

我解决了这个问题。Toast背景色变化的原因是我传递的View对象所在的上下文方式不正确。

以下代码会导致背景色变成意料之外的白色:

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

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

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

我不确定这样修复是否存在很大的问题,因为我还在学习中。如果有人发现了问题,请分享一下。不过看起来似乎工作得很好。


2
这非常有帮助。 - FtheBuilder
在我的情况下,我使用了自定义布局,并通过设置Toast的TextView背景来解决了问题。 - Reejesh PK

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();

-1
Winthrop的回答中还有一个补充。可以将文本框的背景颜色设置为透明,这样烤面包就像原来的半透明烤面包一样。
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();
    }

-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();
    }

并在 drawable 文件夹中添加了 custom_toast.xml

<?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>

非常感谢!


-2

试试这个:

toast.getView().setBackgroundColor(0xFF00ddff);

2
你能否写得更详细一些,不要只写一行代码?请加入一些上下文... - Alexander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接