Toast 更改Toast顯示位置,顯示帶有圖片的吐司,自定義吐司
阿新 • • 發佈:2018-11-09
更改Toast的顯示位置:
toast.setGravity(Gravity.CENTER,0,0); //接受返回物件後的設定
顯示帶有圖片的Toast
Toast toast = Toast.makeText(this, "吐司", Toast.LENGTH_SHORT); LinearLayout toast_layout = (LinearLayout)toast.getView(); // 獲取toast的檢視 ImageView iv = new ImageView(this); iv.setImageResource(R.mipmap.ic_launcher_round); //新增圖片 toast_layout.addView(iv,0); //index是位置 toast.show();
自定義Toast
//先自定義一個佈局檔案 為toast.xml ,什麼樣都可以
View toast_view = LayoutInflater.from(this).inflate(R.layout.toast,null);
Toast toast = new Toast(this);
toast.setView(toast_view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();