美文网首页
Android工作整理-自定义Toast

Android工作整理-自定义Toast

作者: zhongcx | 来源:发表于2020-07-14 17:48 被阅读0次

原因:
1、有些手机多次点击toast会弹出多个toast,会以队列的形式显示。体验不好。
2、系统默认的toast的位置太固定了
3、要改动toast样式

所以,封装了一个工具类



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import io.agora.openacall.R;

/**
 * 自定义toast
 * https://www.jianshu.com/p/4a37c61697dd
 */
public class MyToast {
    private static Toast mToast;

    public static void showToast(Activity activity, String toastMsg) {
        try {
            if (activity == null) {
                return;
            }
            if (TextUtils.isEmpty(toastMsg)) {
                return;
            }
            if (mToast != null) {
                mToast.cancel();
                mToast = null;
            }
            mToast = new Ftoast(activity, toastMsg, Toast.LENGTH_SHORT);//自定义
            mToast.show();
        } catch (Exception e) {
            e.printStackTrace();
            //可能在子线程调了这个方法
        }
    }

    //使用自定义toast
    private static class Ftoast extends Toast {

        public Ftoast(Context context, String msg, int duration) {
            super(context);
            LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflate != null;
            @SuppressLint("InflateParams") View root = inflate.inflate(R.layout.my_toast, null);//加载自定义的XML布局
            TextView txtContent = (TextView) root.findViewById(R.id.txtToast);
            txtContent.setText(msg);

            setDuration(duration);
            setView(root); //这是setView。就是你的自定义View
            //必须设置Gravity.FILL_HORIZONTAL 这个选项,布局文件的宽高才会正常显示
            setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0); //这是,放着顶部,然后水平放满屏幕
        }

    }
}

my_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/acl_record_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">


    <TextView
        android:id="@+id/txtToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_toast"
        android:ellipsize="end"
        android:gravity="center"
        android:paddingBottom="34dp"
        android:paddingLeft="60dp"
        android:paddingRight="60dp"
        android:paddingTop="34dp"
        android:text="test"
        android:textColor="#ffffff"
        android:textSize="36dp" />

</LinearLayout>

bg_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充的颜色 -->
    <solid android:color="#000000" />
    <!-- 设置按钮的四个角为弧形 -->
    <corners android:radius="16dp" />
</shape>

相关文章

网友评论

      本文标题:Android工作整理-自定义Toast

      本文链接:https://www.haomeiwen.com/subject/pvmfhktx.html