Android中我們常常會使用Toast作為提醒的一種方式,可是,Toast不僅能實現最普通的提醒,還能實現很多種自己定制的效果。
1.默認效果
代碼
Toast.makeText(getApplicationContext(), "默認Toast樣式",
Toast.LENGTH_SHORT).show();
2.自定義顯示位置效果
代碼
toast = Toast.makeText(getApplicationContext(),
"自定義位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3.帶圖片效果
代碼
toast = Toast.makeText(getApplicationContext(),
"帶圖片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();