android 自定义Toast以及Toast覆盖显示

1.自定义Toast布局文件 toast_show.xmll

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000">
<LinearLayout
    android:id="@+id/ll_toast"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:gravity="center"
    >
    <TextView
        android:id="@+id/tv_toast"
        android:textSize="18sp"
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

</RelativeLayout>

1.ToastUtils工具类

public class ToastUtils {

    protected static Toast toast   = null;
    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;

    public static void showToast(Context context, String flag){

        View layout = View.inflate(context, R.layout.toast_show, null);//加载一个自定义的toast布局文件
        //获取自定义布局的控件
        final LinearLayout ll_toast = layout.findViewById(R.id.ll_toast);
        final TextView tv_toast = layout.findViewById(R.id.tv_toast);
        if(toast==null){
            toast = new Toast(context);//创建一个Toast示例
            toast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
            if (flag.equals("Y")){
                ll_toast.setBackgroundResource(R.drawable.toast_bg);
                tv_toast.setText("hello world"+flag);
            }else {
                ll_toast.setBackgroundResource(R.drawable.toast_bg2);
            }

            toast.setView(layout);//给toast设置布局
            toast.setGravity(Gravity.FILL, 0, 0);//设置toast在屏幕中的显示位置
            toast.show();
            oneTime=System.currentTimeMillis();
        }else{
            twoTime=System.currentTimeMillis();
            if(flag.equals(oldMsg)){
                if(twoTime-oneTime>Toast.LENGTH_SHORT){
                    toast.show();
                }
            }else{
                oldMsg = flag;
                if (flag.equals("Y")){
                    ll_toast.setBackgroundResource(R.drawable.toast_bg);
                    tv_toast.setText("hello world"+flag);
                }else {
                    ll_toast.setBackgroundResource(R.drawable.toast_bg2);
                    tv_toast.setText("============"+flag);
                }
                toast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
                toast.setView(layout);//给toast设置布局
                toast.setGravity(Gravity.FILL, 0, 0);//设置toast在屏幕中的显示位
                toast.show();
            }
        }
        oneTime=twoTime;
    }
}

3.ActivityMain.java中点击事件引用

 private  static long  lastTime=0;
    public void onClickToast(View v){
       final long nowTime= SystemClock.uptimeMillis();
        if (nowTime-lastTime>10000){
            ToastUtils.showToast(context, "Y");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast(context, "N");
                }
            },1000); 
        }else {
            lastTime=nowTime;
        }
    }

你可能感兴趣的:(android 自定义Toast以及Toast覆盖显示)