java 如何在android中为Toast自定义背景、背景颜色和文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17800953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize background, background color and text color for Toast in android
提问by Aoyama Nanami
I want to customize my toast without creating a custom layout by modifying the default Toast. I want red color for toast's background, and white color for toast's text color and I want to make my toast's background bigger that default toast. when I run my application, there's nothing change from my toast, it still show in default toast.
我想通过修改默认 Toast 来自定义我的 Toast,而不创建自定义布局。我想要吐司背景的红色,吐司文本颜色的白色,我想让我的吐司背景比默认吐司大。当我运行我的应用程序时,我的 toast 没有任何变化,它仍然显示在默认的 toast 中。
This is how I customize my toast:
这是我自定义吐司的方式:
if (seriesSelection == null) {
Toast toast = Toast.makeText(getApplicationContext(), "tidak ada chart yang dipilih", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 50, 50);
toast.getView().setPadding(10, 10, 10, 10);
toast.getView().setBackgroundColor(Color.RED);
TextView text = (TextView) toast.getView().findViewById(android.R.id.message);
text.setTextColor(Color.WHITE);
text.setTextSize(14);
} else {
Toast toast= Toast.makeText(
getApplicationContext(),
"Nilai " + listData.get(seriesSelection.getPointIndex()).getInuNilai()+
" tanggal " + listData.get(seriesSelection.getPointIndex()).getTanggal(),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 50, 50);
toast.getView().setPadding(10, 10, 10, 10);
toast.getView().setBackgroundColor(Color.RED);
text.setTextColor(Color.WHITE);
text.setTextSize(14);
toast.show();
}
回答by Raghunandan
You can have a custom view inflate a custom view and use toast.setView(layout)
.
您可以让自定义视图膨胀自定义视图并使用toast.setView(layout)
.
Example:
例子:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
And your xml
还有你的 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#DAAA"
>
<ImageView android:src="@drawable/droid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
/>
</LinearLayout>
More info @
更多信息 @
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Ran your if and else part of the code (separately) it shows toast with red background and white text color. I don't see any problem. But if you need to customize you can use a custom layout and inflate the layout and set the view to the toast.
运行 if 和 else 部分代码(单独),它显示带有红色背景和白色文本颜色的吐司。我看不出有什么问题。但是如果您需要自定义,您可以使用自定义布局并扩展布局并将视图设置为 toast。
Edit:
编辑:
Your textview
你的文本视图
TextView text = (TextView) toast.getView().findViewById(android.R.id.message);
is initialized in the if part and in else part textview is not initialized.
在 if 部分初始化,在 else 部分 textview 未初始化。
Initialize textview outside if and else code.
在 if 和 else 代码之外初始化 textview。
Check this library called crouton which you might find usefull
检查这个名为 crouton 的库,您可能会发现它很有用
回答by Kevin
回答by Jatin Raghav
I have very simple and easy code for customize a Toast accordingly, you can change background of toast and text color also.
我有非常简单易行的代码来相应地自定义 Toast,您也可以更改 Toast 的背景和文本颜色。
Toast toast = Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG);
View view = toast.getView();
view.setPadding(20, 20, 20, 20);
view.setBackgroundResource(R.color.GREEN);
view.setTextColor(Color.RED);
toast.show();