Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。 1.默认效果:
代码:[li]Toast.makeText(getApplicationContext(), "默认Toast样式",[/li][li] Toast.LENGTH_SHORT).show();
[/li]
复制代码
2.自定义显示位置效果:
代码:
[li]toast = Toast.makeText(getApplicationContext(),[/li][li] "自定义位置Toast", Toast.LENGTH_LONG);[/li][li] toast.setGravity(Gravity.CENTER, 0, 0);[/li][li] toast.show();
[/li]
复制代码
3.带图片效果:
代码
[li]toast = Toast.makeText(getApplicationContext(),[/li][li] "带图片的Toast", Toast.LENGTH_LONG);[/li][li] toast.setGravity(Gravity.CENTER, 0, 0);[/li][li] LinearLayout toastView = (LinearLayout) toast.getView();[/li][li] ImageView imageCodeProject = new ImageView(getApplicationContext());[/li][li] imageCodeProject.setImageResource(R.drawable.icon);[/li][li] toastView.addView(imageCodeProject, 0);[/li][li] toast.show();
[/li]
复制代码
4.完全自定义效果:
代码[li]LayoutInflater inflater = getLayoutInflater();[/li][li] View layout = inflater.inflate(R.layout.custom,[/li][li] (ViewGroup) findViewById(R.id.llToast));[/li][li] ImageView image = (ImageView) layout[/li][li] .findViewById(R.id.tvImageToast);[/li][li] image.setImageResource(R.drawable.icon);[/li][li] TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);[/li][li] title.setText("Attention");[/li][li] TextView text = (TextView) layout.findViewById(R.id.tvTextToast);[/li][li] text.setText("完全自定义Toast");[/li][li] toast = new Toast(getApplicationContext());[/li][li] toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);[/li][li] toast.setDuration(Toast.LENGTH_LONG);[/li][li] toast.setView(layout);[/li][li] toast.show();
[/li]
复制代码
5.其他线程:
代码:
[li]new Thread(new Runnable() {[/li][li] public void run() {[/li][li] showToast();[/li][li] }[/li][li] }).start();
[/li] |