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();
    }
}

 

 

  

posted @ 2020-01-07 10:49  SpringCore  阅读(1377)  评论(0编辑  收藏  举报