Android 自定义Toast

2020-02-28  本文已影响0人  GODANDDEVIL

1、创建一个drawable资源xml文件,用作toast的背景,命名为:toast_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="15dp"/>
    <solid android:color="@color/colorToastBackground"/>
</shape>

2、创建一个自定义Toast的layout文件,命名为:toast_layout.xml

<?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="@drawable/toast_background">

    <TextView
        android:id="@+id/toast_text"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/colorWhite"
        android:padding="20dp"/>

</RelativeLayout>

3、创建一个自定义Toast的类CustomToast继承自Toast

/**
 * 自定义Toast
 */
public class CustomToast extends Toast {
    /**
     * Toast单例
     */
    private static CustomToast toast;
    /**
     * 构造方法
     */
    private CustomToast(Context context) {
        super(context);
    }
    /**
     * 隐藏当前Toast
     */
    private static void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }
    public void cancel() {
        try {
            super.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 重写show方法
     */
    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    text的类型之所以为int是因为要传入strings.xml里的资源字符串
    private static void initToast(Context context, int text) {
        try {
            cancelToast();
            toast = new CustomToast(context);
            // 获取LayoutInflater对象
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 由layout文件创建一个View对象
            View layout = inflater.inflate(R.layout.toast_layout, null);
            // 吐司上的文字
            TextView toast_text = layout.findViewById(R.id.toast_text);
            toast_text.setText(text);
            toast.setView(layout);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 显示Toast
     *
     * @param context 上下文
     * @param text    显示的文本
     * @param time    显示时长
     */
    private static void showToast(Context context, int text, int time) {
        // 初始化一个新的Toast对象
        initToast(context, text);
        // 设置显示时长
        if (time == Toast.LENGTH_LONG) {
            toast.setDuration(Toast.LENGTH_LONG);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        // 显示Toast
        toast.show();
    }
    static void showText(Context context, int text, int time) {
        showToast(context, text, time);
    }
}

使用:CustomToast.showText(context,str,time);

上一篇下一篇

猜你喜欢

热点阅读