Android-特效-Toast

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。

 

1.默认效果:

1 Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_SHORT).show();

 

2.自定义显示位置效果:

1 toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", Toast.LENGTH_LONG);
2 toast.setGravity(Gravity.CENTER, 0, 0);
3 toast.show();

 

3.带图片效果:

1 toast = Toast.makeText(getApplicationContext(), "带图片的Toast", Toast.LENGTH_LONG);
2 toast.setGravity(Gravity.CENTER, 0, 0);
3 LinearLayout toastView = (LinearLayout) toast.getView();
4 ImageView imageCodeProject = new ImageView(getApplicationContext());
5 imageCodeProject.setImageResource(R.drawable.icon);
6 toastView.addView(imageCodeProject, 0);
7 toast.show();

 

4.完全自定义效果:

 1 LayoutInflater inflater = getLayoutInflater();
 2 View layout = inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast));
 3 ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
 4 image.setImageResource(R.drawable.icon);
 5 TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
 6 title.setText("Attention");
 7 TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
 8 text.setText("完全自定义Toast");
 9 toast = new Toast(getApplicationContext());
10 toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
11 toast.setDuration(Toast.LENGTH_LONG);
12 toast.setView(layout);
13 toast.show();

 

5.其他线程:

1 new Thread(new Runnable() {
2     public void run() {
3         showToast();
4     }
5 }).start();

 

转自:http://android.tgbus.com/Android/tutorial/201103/346236.shtml

posted on 2014-06-29 16:22  风一样的胖子  阅读(33)  评论(0)    收藏  举报