Android原生控件 — Toast(弹出组件)

⒈用途

Toast是一个消息提示组件
可以设置显示的位置(自己有默认位置)
自定义显示内容(例如:添加一个图片)
简单封装

⒉使用

  默认

Toast.makeText(getApplicationContext(),"",Toast.LENGTH_LONG).show();

  居中弹出

        Toast toastCenter = Toast.makeText(getApplicationContext(),"",Toast.LENGTH_LONG);
        toastCenter.setGravity(Gravity.CENTER,0,0);
        toastCenter.show();

  带图片的消息弹出

        Toast toastCustom = new Toast(getApplicationContext());
        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        View view = inflater.inflate(R.layout.layout_toast,null);
        ImageView imageView = view.findViewById(R.id.iv_toast);
        TextView textView = view.findViewById(R.id.tv_toast);
        imageView.setImageResource(R.drawable.icon_smile);
        textView.setText("自定义Toast");
        toastCustom.setView(view);
        toastCustom.setDuration(Toast.LENGTH_LONG);
        toastCustom.show();

  简单封装

public class ToastUtil {
    public static Toast mToast;
    public static void showMsg(Context context,String msg){
        if(mToast == null){
            mToast = Toast.makeText(context,msg,Toast.LENGTH_LONG);
        }else{
            mToast.setText(msg);
        }
        mToast.show();
    }
}

  

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注