如何在安卓中自定义Toast的背景、背景颜色和文本颜色

8

我想要在不创建自定义布局的情况下通过修改默认Toast来自定义我的Toast。我希望Toast的背景颜色为红色,文本颜色为白色,并且我想要让我的Toast背景比默认Toast更大。当我运行应用程序时,我的Toast没有任何改变,它仍然显示为默认Toast。

这是我如何自定义我的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();
}

对我来说,“else”情况显示红色烤面包和白色文本,应用填充。 - sandrstar
3个回答

10

您可以使用自定义视图来填充自定义视图并使用 toast.setView(layout)

示例:

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

以及你的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>

更多信息 @

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

你分别运行了代码中的if和else部分,它们显示了红色背景和白色文本颜色的toast。我没有看到任何问题。但如果您需要自定义,则可以使用自定义布局并填充布局,然后将视图设置为toast。

编辑:

你的TextView

  TextView text = (TextView) toast.getView().findViewById(android.R.id.message);

在if部分初始化,在else部分TextView未被初始化。

在if和else代码块之外初始化TextView。

请检查这个名为crouton的库,您可能会发现它很有用。

https://github.com/keyboardsurfer/Crouton


1
我想要自定义我的Toast,而不是通过修改默认的Toast来创建自定义布局。我可以吗? - Aoyama Nanami
1
@AoyamaNanami 你可以检查上面的帖子。另外,你的代码工作得很好,我可以看到有红色背景和白色文本的吐司(Toast)。那么问题确切在哪里?我无法复现你的问题。我通过复制粘贴相同的代码运行了它,它可以正常工作。使用自定义布局将为您提供更多自定义选项。 - Raghunandan

2

Toast有一个setView()方法。

你可以自定义Toast来显示任何视图。

相较于尝试编辑Toast内部的视图,我建议您只需创建一个视图并将其插入即可。


你能告诉我如何自定义它的方式吗? - Aoyama Nanami

-1

我有非常简单易懂的代码,可以根据需要自定义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();

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