欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义Toast

程序员文章站 2024-03-05 11:02:00
...

我相信你一定会用系统自带的Toast,否则你就不会看到这篇文章了。正是由于系统自带的(往往是显示在屏幕的底部)很多时候并不能满足我们的需求所以才需要自定义。好了不多说直接看代码吧。
首先是自定义的工具类

public class ToastUtil {

    private Toast mToast;
    private TextView mTextView;
    private TimeCount timeCount;
    private String message;
    private Handler mHandler = new Handler();
    private boolean canceled = true;

    public ToastUtil(Context context, int layoutId, String msg) {
        message = msg;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //自定义布局
        View view = inflater.inflate(layoutId, null);
        //自定义toast文本
        mTextView = (TextView) view.findViewById(R.id.toast_msg);
        mTextView.setText(msg);
        Log.i("ToastUtil", "Toast start...");
        if (mToast == null) {
            mToast = new Toast(context);
            Log.i("ToastUtil", "Toast create...");
        }
        //设置toast居中显示
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.setView(view);
    }

    /**
     * 自定义居中显示toast
     */
    public void show() {
        mToast.show();
        Log.i("ToastUtil", "Toast show...");
    }

    /**
     * 自定义时长、居中显示toast
     *
     * @param duration
     */
    public void show(int duration) {
        timeCount = new TimeCount(duration, 1000);
        Log.i("ToastUtil", "Toast show...");
        if (canceled) {
            timeCount.start();
            canceled = false;
            showUntilCancel();
        }
    }

    /**
     * 隐藏toast
     */
    private void hide() {
        if (mToast != null) {
            mToast.cancel();
        }
        canceled = true;
        Log.i("ToastUtil", "Toast that customed duration hide...");
    }

    private void showUntilCancel() {
        if (canceled) { //如果已经取消显示,就直接return
            return;
        }
        mToast.show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("ToastUtil", "Toast showUntilCancel...");
                showUntilCancel();
            }
        }, Toast.LENGTH_LONG);
    }

用法也不复杂

 ToastUtil util= new ToastUtil(MainActivity.this,R.layout.toast_short_layout,"Setting successfully");
            util.show();

但是有个小问题 就是你传进来的布局不能只有一个根布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/px270dp"
    android:layout_height="@dimen/px80dp"
    android:orientation="vertical"
    android:background="@drawable/toast_bg">

        <TextView
            android:id="@+id/toast_msg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="@dimen/px24sp"
            android:textColor="@color/color_ffffff"/>
  
</LinearLayout>

这样是达不到预期效果的。原理嘛 暂时没找到为什么。解决的办法就是暂时先加一层布局就好

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/px270dp"
    android:layout_height="@dimen/px80dp"
    android:orientation="vertical"
    android:background="@drawable/toast_bg">
    <RelativeLayout
        android:layout_width="@dimen/px270dp"
        android:layout_height="@dimen/px80dp">
        <TextView
            android:id="@+id/toast_msg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="@dimen/px24sp"
            android:textColor="@color/color_ffffff"/>
    </RelativeLayout>
</LinearLayout>

这样就可以了。问题先留在这,等我以后找到了答案再来改写O(∩_∩)O哈哈~

相关标签: toast