如何设计自定义Toast消息?

时间:2014-12-15 08:25:19

标签: android toast

我想在我的项目中设置自定义吐司消息,我想要自定义吐司,并在吐司消息中添加背景颜色和图片。 我在很多Android应用程序中看到过这种类型的toast消息。我想在我的应用程序中实现这种类型的吐司。

我点这个链接:customize toast in android

e.g。

enter image description here

3 个答案:

答案 0 :(得分:1)

是的,您可以显示custom toast

您需要做的就是:为toast创建自定义xml。

类似的东西:

<强> my_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mylayout"
    android:orientation="horizontal"
    android:background="@android:color/cyan">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/my_img"
        android:id="@+id/imageview">
    </ImageView>

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1" android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="My message"
        android:textColor="@android:color/black">
    </TextView>

</LinearLayout>

现在,在您的活动中,而不是默认Toast,膨胀此自定义xml。

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.my_toast,
                               (ViewGroup) findViewById(R.id.mylayout));

Toast custToast = new Toast(this);
custToast.setView(view);
custToast.show();

希望它有所帮助。

答案 1 :(得分:1)

您可以从here获得有关Toast的最佳教程。

答案 2 :(得分:0)

你可以通过在xml

中创建toast布局来实现

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="#DAAA">

    <ImageView
        android:id="@+id/toast_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="10dp" />

    <TextView android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#FFF" />

</LinearLayout

充气和设置客人吐司:

LayoutInflater inflater = getLayoutInflater();
                        View layout = inflater.inflate(R.layout.toast_custom,
                                                       (ViewGroup) findViewById(R.id.toast_layout_root));
                        ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
//put your image in the drawable folder
                        image.setImageResource(R.drawable.username_incorrect);
                        TextView text = (TextView) layout.findViewById(R.id.toast_text);
                        text.setText("The user name is incorect!");

                        Toast toast = new Toast(getApplicationContext());
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(layout);
                        toast.show();

&GT;

相关问题