1.private void showToast(){
Toast toast = new Toast(this);
ImageView imageview = new ImageView(this);
LinearLayout layout = new LinearLayout();
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(imageview);
toast.setView(layout);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
2.private void showToast(){
Toast toast = new Toast(this);
LayoutInflater inflater = getLayoutInflater();
View layout = iniflater.inflate(R.layout.custom_toast,null);
toast.setView(layout);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
3.避免内存泄露
Toast.make(MainActivity.this,"Hello",Toast.LENGTH_LONG).show();
如果在toast消失之前,用户在前台进行返回操作,或者直接回到桌面,导致Activity无法被GC销毁,这个Activity
就引起了内存泄露,正确做法:
Toast.make(getApplicationContext(),"Hello",Toast.LENGTH_LONG).show();
getApplicationContext()是整个应用的上下文,不会持有Activity对象.