Toast自定义详解

toast字定义的优点:
1.可以制作符合设计的UI
2.界面的美化
3.位置随意定
4.防止连续点击一直弹出提示

直接上代码

  private static Toast mToast;

    public static void showToast(Context context, String msg) {
    //自定义要显示的 layout.xml 文件
        View view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);
        TextView textView = view.findViewById(R.id.textView1);
        textView.setText(msg);
        //可以防止连续点击
        if (mToast == null) {
            mToast = new Toast(context);
            mToast.setView(view);
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.show();
        } else {
            mToast.setView(view);
            mToast.show();
        }
    }

布局文件也弄上代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_bg"
    android:gravity="center"
    android:layout_gravity="center"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="内容"
        android:textColor="@color/color_fff"/>

</FrameLayout>

完毕!
6666666666666666666

觉得好就点个赞吧!!!!

猜你喜欢

转载自blog.csdn.net/qq_33495943/article/details/78988469