Android--ToastUtils--自定义UI,覆盖旧的toast

平嘉熙
2023-12-01

1.  在Application的onreate()中初始化Toast

package com.sidebar.pro;
import com.sidebar.pro.utils.ToastUtil;

public class BaseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ToastUtil.init(this);
    }
}

2. ToastUtils

    使用:ToastUtils.showToast("");

package com.sidebar.pro.utils;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.sidebar.pro.R;

/**
 * Toast统一管理类
 */
public class ToastUtil {

    private static Context context;
    private static View view;
    private static Toast toast;
    public static Handler mHandler = new Handler(Looper.getMainLooper());

    public static void init(Context c) {
        context = c;
    }

    private static void show(Context context, String text) {
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(context);
        //设置Toast要显示的位置,水平居中并在底部,X轴偏移0个单位,Y轴偏移70个单位,
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, dip2px(context, 80));
        //设置显示时间
        toast.setDuration(Toast.LENGTH_SHORT);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
        }
        TextView textView = view.findViewById(R.id.tv_toast_text);
        textView.setText(text);
        toast.setView(view);
        toast.show();
    }

    /**
     * Toast工具类
     * @param content
     */
    public static void showToast(String content) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            show(context, content);
        } else {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    show(context, content);
                }
            });
        }
    }

   /**
     * dp转px
     */
    static int dip2px(Context context, float f) {
        return (int) ((f * context.getResources().getDisplayMetrics().density) + 0.5f);
    }
}

 3. layout_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_toast_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/bg_black65_30"
    android:gravity="center"
    android:lineSpacingExtra="3dp"
    android:maxWidth="667dp"
    android:paddingLeft="47dp"
    android:paddingTop="15dp"
    android:paddingRight="47dp"
    android:paddingBottom="15dp"
    android:textColor="#ffffff"
    android:textSize="21sp"/>

4. bg_black65_30

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充的颜色 -->
    <solid android:color="#a6000000" />
    <!-- android:radius 圆角的半径 -->
    <corners
        android:radius="30dp"/>
</shape>

 类似资料: