美文网首页
Toast(自定义Toast)

Toast(自定义Toast)

作者: CQ_TYL | 来源:发表于2018-11-23 10:48 被阅读0次

    默认的Toast:

    
    Toast.makeText(ProjectList.this,"数据请求失败",Toast.LENGTH_LONG).show();
    
    
    image

    自定义Toast(自定义图标样式\显示时间\位置):

    效果图:

    image

    代码:

    
    MyUtils.showToast(TestActivity.this,"自定义的Toast");
    
    或者 传入自定义显示时间
    
    MyUtils.showToast(TestActivity.this,"自定义的Toast",3000);
    
    
    
    public class MyUtils {
    
    private static Toasttoast;
    
    private static Handlermhandler =new Handler();
    
    private static Runnable r =new Runnable() {
    
    public void run() {
    
    isShowing=false;
    
            toast.cancel();
    
        }
    
    };
    
    private static BooleanisShowing=false;
    
    public static void showToast(final Context context, final String msg){
    
    if (!isShowing){
    
    if (toast ==null)
    
    toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
    
            toast.setGravity(Gravity.CENTER, 0, 0);
    
            Activity context1 = (Activity) context;
    
            LayoutInflater layoutInflater = context1.getLayoutInflater();
    
            View inflate = layoutInflater.inflate(R.layout.toast_layout, null);
    
            TextView toast_msg = inflate.findViewById(R.id.toast_msg);
    
            toast_msg.setText(msg);
    
            toast.setView(inflate);
    
            toast.show();
    
            isShowing=true;
    
            mhandler.postDelayed(r, 2000);
    
        }
    
    }
    
    public static void showToast(final Context context, final String msg,int time){
    
    if (!isShowing){
    
    if (toast ==null)
    
    toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
    
            toast.setGravity(Gravity.CENTER, 0, 0);
    
            Activity context1 = (Activity) context;
    
            LayoutInflater layoutInflater = context1.getLayoutInflater();
    
            View inflate = layoutInflater.inflate(R.layout.toast_layout, null);
    
            TextView toast_msg = inflate.findViewById(R.id.toast_msg);
    
            toast_msg.setText(msg);
    
            toast.setView(inflate);
    
            toast.show();
    
            isShowing=true;
    
            mhandler.postDelayed(r, time);
    
        }
    
    }
    
    }
    
    
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent"
    
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
                android:layout_width="match_parent"
    
                android:layout_height="match_parent">
    
                        android:padding="8dp"
    
                        android:orientation="vertical"
    
                        android:layout_centerInParent="true"
    
                        android:background="@drawable/toast_shape"
    
                        android:layout_width="wrap_content"
    
                        android:minWidth="80dp"
    
                        android:layout_height="wrap_content">
    
                                android:layout_marginTop="4dp"
    
                                android:layout_gravity="center"
    
                                android:src="@mipmap/tishi"
    
                                android:layout_width="wrap_content"
    
                                android:layout_height="wrap_content" />
    
                                android:id="@+id/toast_msg"
    
                                android:layout_marginTop="6dp"
    
                                android:textSize="14dp"
    
                                android:textColor="@color/white"
    
                                android:layout_gravity="center"
    
                                android:layout_width="wrap_content"
    
                                android:layout_height="wrap_content" />
    
    </RelativeLayout>
    
    

    相关文章

      网友评论

          本文标题:Toast(自定义Toast)

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