Android的各種Toast
Toast僅僅是向用戶做出一個提示,提示有各種各樣的方式。下面將一一道來。
1.最簡單的Toast
Toast.makeText(this, "默認", Toast.LENGTH_SHORT).show();
2.自定義顯示位置的Toast
Toast t; t=Toast.makeText(this, "自定義顯示位置", Toast.LENGTH_SHORT); t.setGravity(Gravity.CENTER, 0, 0); t.show();
3帶圖片的Toast
t=Toast.makeText(this, "帶圖片的Toast", Toast.LENGTH_SHORT); LinearLayout ll=(LinearLayout) t.getView(); ImageView iv=new ImageView(this); iv.setBackgroundResource(R.drawable.ic_launcher); ll.addView(iv,0); t.show();
4.完全自定義的Toast
t=new Toast(this); LayoutInflater li=this.getLayoutInflater(); View vee=li.inflate(R.layout.iii, null); t.setView(vee); t.show();
5.來自其它線程的Toast
Handler h=new Handler(); /////////////////////////////////////////////////// new Thread(){ @Override public void run() { showToast(); } }.start(); //////////////////////////////////////////////////////// protected void showToast() { h.post(new Runnable(){ @Override public void run() { Toast.makeText(getApplicationContext(), "come from other thread", Toast.LENGTH_SHORT).show(); } }); }
來自其它線程的Toast我有一點要說明,h.post(Runnable r);這個方法里的run是在主線程的。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!