一个简单的自定义Toast

项目中,有时系统自带的Toast无法满足要求时,就会需要自定义一些Toast,自己在项目中写了一个简单的自定义Toast,在这里记录下。

 

 

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


import com.demo.demoapp.R;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 自定义提示Toast
 */
public class ToastDialog {

    public static void showSuccessToast(final Activity context, final String message) {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToastDialog(context, message,null);
            }
        });

    }

    public static void showFailToast(final Activity context, final String message) {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToastDialog(context, message,R.drawable.ic_taost_fail);
            }
        });
    }

    public static void showTextToast(final Activity context, final String message) {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToastDialog(context, message,-9999);
            }
        });
    }


    /**
     * 显示Toast
     * @param context
     * @param message
     */
    private static void showToastDialog(Context context, String message,Integer imageId) {
        //加载Toast布局
        View toastRoot = LayoutInflater.from(context).inflate(R.layout.black_toast_dialog, null);
        //初始化布局控件
        ImageView image = (ImageView) toastRoot.findViewById(R.id.black_toast_dialog_image);
        TextView msgView = (TextView) toastRoot.findViewById(R.id.black_toast_dialog_textview);

        if (imageId != null) {
            image.setBackground(context.getResources().getDrawable(imageId));
            msgView.setTextColor(context.getResources().getColor(R.color.white));
            if (imageId == -9999) {
                image.setVisibility(View.GONE);
            }
        }

        //为控件设置属性
        msgView.setText(message);
        //Toast的初始化
        Toast toastStart = new Toast(context);
        //获取屏幕高度
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        //Toast的Y坐标是屏幕高度的1/3,不会出现不适配的问题
        toastStart.setGravity(Gravity.CENTER, 0, 0);
        toastStart.setDuration(Toast.LENGTH_SHORT);
        toastStart.setView(toastRoot);
//        toastStart.show();

        showMyToast(toastStart, 1500);
    }

    /**
     * 设置Toast时间
     * @param toast
     * @param cnt
     */
    private static void showMyToast(final Toast toast, final int cnt) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }

}

 

布局文件:

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:id="@+id/root"
    android:background="@drawable/dialog_black_bg"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/black_toast_dialog_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/ic_success"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"/>

    <TextView
        android:id="@+id/black_toast_dialog_textview"
        android:textSize="25sp"
        android:textColor="#fff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="200dp"
        android:text="成功"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"/>

</LinearLayout>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值