记录一个自定义的Toast

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40543575/article/details/83654311

很简单,项目中有视频播放器,用了一些toast作为提示信息,但是系统的整的太low了
没办法,谁让我追求美感呢!

优点
	1)使用简单
	2)系统自带Toast采用的是队列显示的方式, 等当前Toast消失后, 下一个Toast才能显示出来; 而ToastUtil会把当前Toast顶掉, 直接显示最新的Toast,也可以自定义其他的美观的UI图	

一步一步的实现吧

  1. (一)Layout:

定义一个toast的layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/toast_radius_and"
    android:orientation="vertical">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingBottom="@dimen/base10dp"
        android:paddingLeft="@dimen/base12dp"
        android:paddingRight="@dimen/base12dp"
        android:paddingTop="@dimen/base10dp"
        android:shadowRadius="5"
        android:text="message"
        android:textColor="@color/white_base" />

</LinearLayout>

当然,也可以添加一个背景,我比较喜欢爱奇艺的那种toast,圆润又好看

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充的颜色 #859AFB-->
    <solid android:color="#000000" />
    <!-- android:radius 弧形的半径 -->
    <corners android:radius="20dip" />
</shape>


  1. (二)ToastUitls:
public class ToastUtil extends Toast {
    private static ToastUtil toast;

    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public ToastUtil(Context context) {
        super(context);
    }

    /**
     * 显示一个纯文本吐司
     *
     * @param context 上下文
     * @param text    显示的文本
     */
    public static void showText(Context context, CharSequence text) {
        showToast(context, text);
    }

    /**
     * 显示Toast
     *
     * @param context 上下文
     * @param text    显示的文本
     */
    private static void showToast(Context context, CharSequence text) {
        // 初始化一个新的Toast对象
        initToast(context, text);
        // 设置显示时长
        toast.setDuration(Toast.LENGTH_SHORT);
        // 显示Toast
        toast.show();
    }

    /**
     * 初始化Toast
     *
     * @param context 上下文
     * @param text    显示的文本
     */
    private static void initToast(Context context, CharSequence text) {
        try {
            cancelToast();

            toast = new ToastUtil(context);

            // 获取LayoutInflater对象
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 由layout文件创建一个View对象
            View layout = inflater.inflate(R.layout.video_toast, null);
            layout.getBackground().setAlpha(180);
            // 吐司上的文字
            TextView toast_text = (TextView) layout.findViewById(R.id.message);
            toast_text.setText(text);
            toast.setView(layout);
            //设置Toast要显示的位置,水平居中并在底部,X轴偏移0个单位,Y轴偏移180个单位,
            toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 180);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 显示toast
     */
    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception e) {
        }
    }

    /**
     * 隐藏当前Toast
     */
    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }
     /**
     * 当前Toast消失
     */
    public void cancel() {
        try {
            super.cancel();
        } catch (Exception e) {

        }
    }

    /**
     * 显示一个富文本吐司
     *
     * @param context 上下文
     * @param text    显示的文本
     */
    public static void showSpannableText(Context context, CharSequence text) {
        showToast(context, text);
    }

}
  1. 调用方式
ToastUtil.showText(context,"message");

不用担心会弹出很多的toast信息,因为每次弹出新的toast时就会把之前的toast取消掉

呵呵,前任的Toast
public class ToastUtils {
    private static Toast toast;

    public static void showToast(Context context, String message) {
        if (toast == null) {
            toast = new Toast(context).makeText(context, message, Toast.LENGTH_SHORT);
        } else {
            toast.setText(message);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40543575/article/details/83654311