Android 五种 Toast显示样式
一、系统默认Toast显示样式
Toast.makeText(this, "系统默认Toast显示方式", Toast.LENGTH_SHORT).show();
二、自定义Toast显示位置
Toast toast = Toast.makeText(this, "自定义位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
三、自定义Toast带图片显示效果
Toast toast = Toast.makeText(this, "带图片的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imgBack = new ImageView(this); imgBack.setImageResource(R.mipmap.ic_launcher); toastView.addView(imgBack, 0); toast.show();
四、完全自定义显示效果
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast_layout, (LinearLayout) findViewById(R.id.custom_toast_lay)); ImageView image = (ImageView) layout.findViewById(R.id.custom_toast_img); image.setImageResource(R.mipmap.ic_launcher); TextView title = (TextView) layout.findViewById(R.id.custom_toast_title); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.custom_toast_txt); text.setText("完全自定义Toast"); Toast toast = new Toast(this); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
五、其他线程Toast显示
new Thread(newRunnable() { public void run() { Looper.prepare(); showToast(); Looper.loop(); } }).start(); public void showToast () { Toast.makeText(this, "其他线程", Toast.LENGTH_SHORT).show(); }
注:不建议使用Looper,最好是能handler回主线程显示Toast