如何在安卓中更改默认的提示信息颜色和背景颜色?

67

我想创建一个白色背景、黑色文字的提示信息。我的提示信息是:

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

我希望用另一种方法创建它,而不是在onCreate()中创建。


使用此网站自定义Toast:http://android-arsenal.com/free?sort=name - Anand Savjani
注意,Android 11 中 Toast 的更新。 - Anoop M Maddasseri
16个回答

120

您可以按以下方式创建自定义的提示消息:

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

4
谢谢,这个应该是被采纳的答案。 - ClassA
1
自定义 Toast 视图已被弃用。使用 Toast(Context) 构造的 Toast,如果没有调用 setView(View) 并传入非空视图,则会返回 null。 - Aydinozkan
@Aydinozkan 但是在我的回答中,我们已经使用了非空视图来调用setView(view),对吧? - Android Killer
@AndroidKiller 你是对的,我的错误。 - Aydinozkan
2
谷歌在Android 11中已经弃用了它,现在我们只能使用原始的Toast! - Akshay Ashok
显示剩余4条评论

87

不需要额外布局即可更改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();

4
适合我的需求。 - Nouman Ch
自定义的 Toast 视图已被弃用。使用 Toast(Context) 构造的 Toast,如果没有调用 setView(View) 方法并传递非 null 的视图,则会返回 null。 - Aydinozkan
如果您想使用colors.xml和dimensions.xml中的颜色和文本大小资源,您应该编写什么代码? - Steven
暂时弃用。 - Eenvincible
看起来 setColorFilter() 已经被弃用了,在 Android 11(api30) 上运行我的安卓应用时会导致崩溃。 - Hamid Habibi
显示剩余2条评论

42

《Android 11》中的 Heads Up,Toast 更新

为保护用户和维护良好的用户体验,Android 11 在阻止后台应用发送自定义 Toast 上下功夫了。因此,如果一个应用程序是针对 Android 11 的,则系统会阻止该应用程序从后台发送包含自定义视图的 Toast 消息,以确保安全。

在 Android R 中新增了 addCallback() 方法,可以用来在 toast(文本或自定义)出现或消失时进行通知。

Toast API 更改中最重要的一点是:对于针对 Android 11 的应用程序,在访问 getView() 方法时将返回 null 值。所以,请确保保护您的应用程序免受致命异常的影响,您懂我的意思 :)


您可以使用以下代码自定义 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();
    }
}
 

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


3
这是最好的解决方案。 - user2587965
1
如何在setView被弃用时更改Android 11的背景。 - Wirling
有没有办法设置字体? - Ali Zarei
1
您的链接“toast API更改”显示404错误。 - androidguy
@androidguy 已更新,谢谢。 - Anoop M Maddasseri

13

要更改默认的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的颜色没有改变? - Yugesh
1
自定义 Toast 视图已被弃用。使用 Toast(Context) 构造的 Toast,如果没有调用 setView(View) 并传入非空视图,则会返回 null。 - Aydinozkan
如果您想使用colors.xml和dimensions.xml中的颜色和文本大小资源,您应该编写什么代码? - Steven

12

使用这种方式

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

3
改变背景色和文字颜色后,烤面包的形状变成了矩形。为什么会这样?改变颜色后为什么不能保留烤面包默认的椭圆形状? - K Pradeep Kumar Reddy
2
改变颜色后,Toast 变成了正方形。为什么? - garish
自定义 Toast 视图已被弃用。使用 Toast(Context) 构造的 Toast,如果没有调用 setView(View) 并传入非空视图,则会返回 null。 - Aydinozkan
不适用于Android11。 - Ali Azaz Alam
getview已过时 - famfamfam

7
创建一个名为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();         
    }

2
为什么两个变量的名称都是 toast - shareef

5
Toast toast=   Toast.makeText(YOUR ACTIVITY NAME ,Toast.LENGTH_SHORT);
View view =toast.getView();
view.setBackgroundColor(Color.GREEN); //any color your want 
toast.show();

5
为了保持圆角化。
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。 - user12755203
getview已过时 - famfamfam

5

如何在JAVA中更改默认的烤面包消息颜色和背景颜色。您可以通过以下方式更改烤面包消息颜色和背景颜色。

        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形状变成了矩形。为什么会发生这种情况?为什么在更改颜色后默认的椭圆形状不能保留? - K Pradeep Kumar Reddy
getview已过时 - famfamfam

4

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

getview已过时 - famfamfam

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