android - 如何在 android 中更改默认的 toast 消息颜色和背景颜色?

标签 android android-toast

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

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

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

最佳答案

您可以创建如下的自定义 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();

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

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

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

关于android - 如何在 android 中更改默认的 toast 消息颜色和背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31175601/

相关文章:

android - openCV4Android 平滑校正透视

java - Android Studio : Add number every Second

android - 删除 Android PopupWindow 中的阴影

android - Anko toast() 方法在从 Fragment 调用时导致 java.lang.NoSuchMethodError

android Espresso-toast 消息断言不适用于 sdk 30

android - toast.getView() 和 toast.setView() 已弃用

android - 如果已经显示了一个 Toast,如何避免 Toast

android - 如何将视频文件下载到 SD 卡?

java - 避免忽略 "/"之后的下一个字符

android - 如何从 IntentService 创建 toast ?它卡在屏幕上