如何在Android中自定义Toast的背景,背景颜色和文本颜色

时间:2013-07-23 03:19:03

标签: java android 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();
}

3 个答案:

答案 0 :(得分:9)

您可以让自定义视图为自定义视图充气并使用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部分代码(单独)展示出来,用红色背景和白色文字颜色显示吐司。我没有看到任何问题。但是如果你需要自定义,你可以使用自定义布局并使布局膨胀并将视图设置为吐司。

编辑:

您的textview

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

在if部分初始化,而在其他部分textview未初始化。

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

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

https://github.com/keyboardsurfer/Crouton

答案 1 :(得分:2)

Toast有setView()方法。

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

我要说的是,您只需创建一个View并自行弹出,而不是尝试编辑Toast中的视图。

答案 2 :(得分:0)

我有相当简单易用的自定义Toast的代码,你也可以改变toast和text color的背景。

 Toast toast = Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG);
    View view1 = toast.getView();
    toast.getView().setPadding(20, 20, 20, 20);
    view1.setBackgroundResource(R.color.GREEN);
    view1.setTextColor(Color.RED);
    toast.show();
相关问题