Android 自定义Toast

为了满足产品的各种需求以及美观度,有时候系统的东西不能满足时,就需要自定义了,

今天要说的是自定义Toast,下面我就直接贴代码吧,如果喜欢记得给妹子点个赞哦O(∩_∩)O~~

如有不对的地方,望各路小哥哥,小姐姐,指导

效果图:(因为我每次看别人的博客,首先是想要看效果的,所有我写博客也是尽量先上图)我这里是设置的居中显示

Android 自定义Toast

 

首先是需要用到的布局,这里我只写我自己用到的布局:

1.toast_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/toast_bg"
        android:gravity="center"
        android:minWidth="100dp"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="10dp" >

        <ImageView
            android:id="@+id/iv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/color_white"
            android:textSize="15dp" />
    </LinearLayout>



</LinearLayout>

背景图:toast_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#c8333333" />

    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />

</shape>

 

 

2.ToastView

package com.mobivans.wealth.customerview;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mobivans.wealth.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: 潇潇
 * @create on:  2019/4/8
 * @describe:自定义ToastView
 */

public class ToastView {
    public static Toast toast;
    private int time;
    private Timer timer;


    public ToastView(Context context, String text) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast_view, null);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
    }

    public ToastView(Context context, int text, boolean hasImg, int resId) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast_view, null);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_title);
        imageView.setImageResource(resId);
        imageView.setVisibility(View.VISIBLE);

        t.setGravity(Gravity.CENTER);
        t.setText(text);
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
    }

    public ToastView(Context context, String text, boolean hasImg, int resId) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast_view, null);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_title);
        imageView.setImageResource(resId);
        imageView.setVisibility(View.VISIBLE);

        t.setGravity(Gravity.CENTER);
        t.setText(text);
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
    }

    // 设置toast显示位置
    public void setGravity(int gravity, int xOffset, int yOffset) {
        toast.setGravity(Gravity.CENTER, 0, 0); // 居中显示
        // toast.setGravity(gravity, xOffset, yOffset);
    }

    // 设置toast显示时间
    public void setDuration(int duration) {
        toast.setDuration(duration);
    }

    // 设置toast显示时间(自定义时间)
    public void setLongTime(int duration) {
        // toast.setDuration(duration);
        time = duration;
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                if (time - 1000 >= 0) {
                    show();
                    time = time - 1000;
                } else {
                    timer.cancel();
                }
            }
        }, 0, 1000);
    }

    public void show() {
        toast.show();
    }

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
        }
    }


}

3.ToastUtils

package com.mobivans.wealth.utils;

import android.content.Context;
import android.view.Gravity;

import com.mobivans.wealth.customerview.ToastView;

/**
 * @author: 潇潇
 * @create on:  2019/4/8
 * @describe:DOTO
 */

public class ToastUtil {

    public static ToastView showCenter(Context context, String message) {
        ToastView toastView = new ToastView(context, message);
        toastView.setGravity(Gravity.CENTER, 0, 0);
        toastView.show();
        return toastView;
    }

    public static ToastView showCenter_img(Context context, String message, boolean hasImg, int resId) {
        ToastView toastView = new ToastView(context, message, hasImg, resId);
        toastView.setGravity(Gravity.CENTER, 0, 0);
        toastView.show();
        return toastView;
    }


}

4.在activity的调用

ToastUtil.showCenter(ToastActivity.this, "toast1");
ToastUtil.showCenter_img(ToastActivity.this, "toast2", true, R.drawable.wujiaoxing);