【问题标题】:Toast background color being changedToast 背景颜色正在更改
【发布时间】:2012-12-24 00:53:45
【问题描述】:

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

<!-- 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(具有默认值)淡入时截取的。

【问题讨论】:

    标签: android xml styles toast


    【解决方案1】:

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

    以下代码行会导致背景颜色变为不需要的白色:

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

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

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

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

    【讨论】:

    • 这很有帮助
    • 在我的例子中,我使用的是自定义布局,并通过设置 toast 文本视图的背景来解决问题。
    【解决方案2】:

    对于我来说,使用 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();
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        这对我有用。我拿了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();
            }
        

        并在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>
        

        非常感谢!

        【讨论】:

          【解决方案5】:

          试试这个:

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

          【讨论】:

          • 你能不能写多一点,而不只是单行字?请添加一些上下文,...
          猜你喜欢
          • 2019-07-21
          • 2015-09-19
          • 2013-08-08
          • 1970-01-01
          • 2018-03-04
          • 1970-01-01
          相关资源
          最近更新 更多