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

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM