【问题标题】:How can I change default toast message color and background color in android?如何在 android 中更改默认的 toast 消息颜色和背景颜色?
【发布时间】:2015-09-19 10:52:53
【问题描述】:

我想创建一个背景颜色为白色且消息颜色为黑色的 toast 消息。我的祝酒词是:

Toast.makeText(Logpage.this, "Please Give Feedback...", 3000).show();

我想用另一种方法创建它,而不是onCreate()

【问题讨论】:

标签: android android-toast


【解决方案1】:

您可以创建如下的自定义 toast 消息:

Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = inflater.inflate(R.layout.your_custom_layout, null);
toast.setView(view);
toast.show();

您可以将一个文本视图放入布局文件中,并根据需要提供背景和文本颜色。

您还可以执行以下操作,不需要额外的自定义布局文件:

Toast toast = Toast.makeText(context, R.string.string_message_id, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_background);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*Here you can do anything with above textview like text.setTextColor(Color.parseColor("#000000"));*/
text.setTextColor(Color.parseColor("#000000"));
toast.show();

【讨论】:

  • 谢谢,这应该是公认的答案。
  • 这是最好的答案
  • 不推荐使用自定义 toast 视图。用 Toast(Context) 构造的 Toast 没有调用 setView(View) 的非空视图将返回 null
  • @Aydinozkan 但在我的回答中,我们用非空视图调用了 setView(view),对吧?
  • Google 从 Android 11 中弃用了它,现在我们只能使用原始 Toast!
【解决方案2】:

无需任何额外布局即可更改 Toast 颜色,2018 年

这是我发现更改 Toast 实际图像背景颜色以及文本颜色的一种非常简单的方法,它不需要任何额外的布局或任何 XML 更改:

Toast toast = Toast.makeText(context, message, duration);
View view = toast.getView();

//Gets the actual oval background of the Toast then sets the colour filter
view.getBackground().setColorFilter(YOUR_BACKGROUND_COLOUR, PorterDuff.Mode.SRC_IN);

//Gets the TextView from the Toast so it can be editted
TextView text = view.findViewById(android.R.id.message);
text.setTextColor(YOUR_TEXT_COLOUR);

toast.show();

【讨论】:

  • 非常适合我的要求。
  • view.setBackgroundColor(bgColor) 对我来说很有趣
  • 我没有做成椭圆形:(有没有办法解决?
  • 不推荐使用自定义 toast 视图。用 Toast(Context) 构造的 Toast 没有调用 setView(View) 的非 null 视图将返回 null
  • 如果要分别使用colors.xml和dimensions.xml中的颜色资源和文字大小资源怎么写?
【解决方案3】:

Heads Up, Updates to toasts in Android 11

来自后台的自定义 toast 被阻止,Android 11 提供保护 用户通过弃用自定义 toast 视图。出于安全原因和 保持良好的用户体验,系统会阻止包含 如果这些 toasts 是由应用程序从后台发送的,则自定义视图 以 Android 11 为目标。

addCallback() 在 Android R 中添加的方法如果您想在 toast(文本或自定义)出现或消失时收到通知。

toast API changes 中最重要的文字,对于面向 Android 11 的应用getView() 方法在您访问它时返回 null,因此,请确保保护您的应用免受 FATAL EXCEPTION 的影响,您知道的我的意思:)


您可以使用以下代码自定义android原生toast

/**
 * ShowToast
 */
public class ShowToast {
    public ShowToast(Context context, String info) {
        Toast toast = Toast.makeText(context, Html.fromHtml("<font color='#e3f2fd' ><b>" + info + "</b></font>"), Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
    }
}
 

如果你想改变背景,你必须在 toast 中使用自定义布局

【讨论】:

  • 这是最好的解决方案
  • setView为deprecated时如何在Android 11上更改背景
  • 有没有办法设置字体?
  • 您的链接“toast API 更改”给出 404。
  • @androidguy 已更新,谢谢。
【解决方案4】:

要更改默认的Toast 文本颜色和背景颜色试试这样。

 Toast toast = Toast.makeText(MainActivity.this, "Please Give Feedback...", Toast.LENGTH_LONG);
 View view = toast.getView();

 //To change the Background of Toast
 view.setBackgroundColor(Color.TRANSPARENT);
 TextView text = (TextView) view.findViewById(android.R.id.message);

 //Shadow of the Of the Text Color
 text.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
 text.setTextColor(Color.BLACK);
 text.setTextSize(Integer.valueOf(getResources().getString(R.string.text_size)));
 toast.show();

【讨论】:

  • @Saranya 你遇到了什么错误或者吐司颜色没有改变。
  • 不推荐使用自定义 toast 视图。用 Toast(Context) 构造的 Toast 没有调用 setView(View) 的非 null 视图将返回 null
  • 如果要分别使用colors.xml和dimensions.xml中的颜色资源和文字大小资源怎么写?
【解决方案5】:

这样用

Toast toast = Toast.makeText(MainActivity.this, R.string.toastMessage, Toast.LENGTH_LONG);   
toast.getView().setBackgroundColor(Color.parseColor("#F6AE2D"));
toast.show();

【讨论】:

  • 更改背景颜色和文本颜色后,吐司形状变为矩形。为什么会这样??为什么更改颜色后没有保留默认的吐司椭圆形状??
  • 更换颜色后,吐司变成乡绅。为什么?
  • 不推荐使用自定义 toast 视图。用 Toast(Context) 构造的 Toast 没有调用 setView(View) 的非空视图将返回 null
  • 不适用于Android11
【解决方案6】:

创建一个布局文件 toast.xml,以了解您的 toast 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a custom toast."
        android:textColor="@android:color/white"
        android:layout_gravity="center_vertical" />

</LinearLayout>

要在 java 文件中显示 toast,请输入以下代码:

public void showCustomAlert()
    {         
        Context context = getApplicationContext();
        // Create layout inflator object to inflate toast.xml file
        LayoutInflater inflater = getLayoutInflater();

        // Call toast.xml file for toast layout 
        View toastView = inflater.inflate(R.layout.toast, null);

        Toast toast = new Toast(context);
        toastView.setView(toast);

        // Set layout to toast 
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
                0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();         
    }

【讨论】:

  • 怎么会有两个变量具有相同的 anem toast
【解决方案7】:
Toast toast=   Toast.makeText(YOUR ACTIVITY NAME ,Toast.LENGTH_SHORT);
View view =toast.getView();
view.setBackgroundColor(Color.GREEN); //any color your want 
toast.show();

【讨论】:

    【解决方案8】:

    在 JAVA 中更改默认 toast 消息颜色和背景颜色。您可以通过这种方式更改 toast 消息颜色和背景颜色..

            Toast toast=Toast.makeText(MainActivity.this,"Signin button is clicked.",Toast.LENGTH_SHORT);
            View view =toast.getView();
            view.setBackgroundColor(Color.GREEN);
            TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
            toastMessage.setTextColor(Color.RED);
            toast.show();
    

    用这种方式更改 toast 文本颜色..

            Toast toast = Toast.makeText(getApplicationContext(), "Signup button is clicked.",Toast.LENGTH_SHORT);
    
            TextView toastMessage=(TextView) toast.getView().findViewById(android.R.id.message);
            toastMessage.setTextColor(Color.BLUE);
            toast.show();
    

    【讨论】:

    • 更改背景颜色和文本颜色后,吐司形状变为矩形。为什么会这样??为什么更改颜色后没有保留toast的默认椭圆形状??
    【解决方案9】:

    Kotlin 版本:

     val toast = Toast.makeText(this, getString(R.string.back_again), Toast.LENGTH_SHORT)
     val view = toast.view
     view.background.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN)
     toast.show()
    

    【讨论】:

      【解决方案10】:

      保持圆角

      val toast = Toast.makeText(context, "Text", Toast.LENGTH_SHORT)
      toast.view.background.setTintList(ContextCompat.getColorStateList(context, android.R.color.darker_gray))
      toast.show()
      

      【讨论】:

      • 这需要 API 级别 21。
      【解决方案11】:
      static void CustomToast(Context context, String text, int duration,
                                          @Nullable Integer backgroundColor,
                                          @Nullable Integer textColor){
              Toast t = Toast.makeText(context,text,duration);
              if (backgroundColor != null)
                  t.getView()
                          .setBackgroundTintList(ColorStateList.valueOf(backgroundColor));
              if (textColor != null)
                  ((TextView)t.getView().findViewById(android.R.id.message))
                          .setTextColor(textColor);
              t.show();
          }
      

      【讨论】:

        【解决方案12】:

        Kotlin 版本:

        val toast:Toast = Toast.makeText(this, "Please enter your name !", Toast.LENGTH_SHORT)
        val view = toast.view
        view?.background?.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
        toast.show()
        

        【讨论】:

        • 不适用于Android11
        【解决方案13】:

        除了@AndroidKiller 的回答,您还可以设置gravity 和自定义TextView 等等:

        Toast toast = Toast.makeText(context, context.getResources().getString(resID), Toast.LENGTH_LONG);
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );        
        View toastView = li.inflate(R.layout.toast_hint_layout, null);
        TextView text = (TextView) toastView.findViewById(R.id.hint_text_tv);
        text.setText(resID);
        toast.setView(toastView);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toastView.setBackgroundResource(R.drawable.toast_9_patch);          
        toast.show();
        

        注意,您的背景可绘制对象应该是nine-patch PNG

        您甚至可以像这样在 XML 中放入一个ImageView 和多个TextView

        <LinearLayout android:id="@+id/layout_root"
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">
            <ImageView
                android:layout_width="32dp"
                android:layout_height="43dp"
                android:src="@drawable/lightbulb"
                />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:orientation="vertical"
                >
                <TextView
                    android:id="@+id/hint_text_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ccc"
                    android:textSize="14dp"
                    />
        
                <TextView
                    android:id="@+id/hint_text_tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="(disable hints in preferences)"
                    android:textColor="#555"
                    android:textSize="11dp"
                    />
            </LinearLayout>
        </LinearLayout>
        

        【讨论】:

          【解决方案14】:

          我可以通过设置背景颜色过滤器颜色并找到 toast 资源 ID 并设置文本颜色来做到这一点。

          Android.Graphics.Color

              /// <summary> Creates and displays a toast with the given text. </summary>
              public void Show(string message)
              {
                  // Create the toast.
                  Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long);
          
                  // Set the background color.
                  Android.Graphics.Color c = new Android.Graphics.Color(122, 193, 66, 255);
                  Android.Graphics.ColorMatrixColorFilter CMF = new Android.Graphics.ColorMatrixColorFilter(new float[]
                      {
                          0,0,0,0,(float)c.R,
                          0,0,0,0,(float)c.G,
                          0,0,0,0,(float)c.B,
                          0,0,0,1,0
                      });
                  toast.View.Background.SetColorFilter(CMF);
          
                  // Set the text color.
                  toast.View.FindViewById<TextView>(Android.Resource.Id.Message).SetTextColor(new Android.Graphics.Color(0, 55, 103, 255));
          
                  // Display the toast.
                  toast.Show();
              }
          

          【讨论】:

            【解决方案15】:

            保持圆形形状的短java代码:

            Toast toast = Toast.makeText(context, "message", Toast.LENGTH_SHORT);
            toast.getView().setBackgroundTintList(ColorStateList.valueOf(Color.RED));
            toast.show();
            

            【讨论】:

            • 不适用于Android11
            【解决方案16】:

            这里你可以更新 toast 类的现有视图 因为设置新背景会移除圆角边框,而​​你想要的只是改变背景颜色和文本颜色而不改变 toast 背景装饰

                    Toast toast = Toast.makeText(getActivity(), "Copied", Toast.LENGTH_LONG);
                    TextView textView = toast.getView().findViewById(android.R.id.message);
                    textView.setTextColor(Color.WHITE);
                    textView.setTextSize(25);
                    toast.getView().getBackground().clearColorFilter();
                    toast.getView().getBackground().setColorFilter(Color.BLACK, PorterDuff.Mode.DARKEN);
                    toast.show();
            

            【讨论】:

            • 虽然这段代码可能会解决问题,包括解释如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释。
            猜你喜欢
            • 2019-07-21
            • 2016-06-17
            • 1970-01-01
            • 2012-12-24
            • 1970-01-01
            • 2018-01-05
            • 2021-11-24
            • 2022-01-15
            • 1970-01-01
            相关资源
            最近更新 更多