吐司背景颜色被改变

问题描述 投票:11回答:5

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

<!-- 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消息时,正常显示。

这真的很奇怪,并希望在这个问题上有任何帮助。

编辑:添加屏幕显示问题。屏幕截图就像初始吐司(带有不良影响)淡出而新的(有默认值)淡入淡出。

android xml styles toast
5个回答
22
投票

我解决了这个问题。 Toast背景颜色更改的原因是由于我在其中包含的View对象的上下文中传递的方式。

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

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

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

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

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


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

0
投票

对恩斯洛普的答案的补充。而不是将文本框的背景颜色设置为#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();
    }

0
投票

这对我有用。我拿了Sachin Murali G' s代码

 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.xml文件夹中添加了drawable

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

非常感谢!


0
投票

试试这个:

toast.getView().setBackgroundColor(0xFF00ddff);
© www.soinside.com 2019 - 2024. All rights reserved.