我想创建一个背景颜色为白色且消息颜色为黑色的 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/