Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。
04 |
Toast.makeText(getApplicationContext(), "默认Toast样式" , |
05 |
Toast.LENGTH_SHORT).show(); |
10 |
toast
= Toast.makeText(getApplicationContext(), |
11 |
"自定义位置Toast" ,
Toast.LENGTH_LONG); |
12 |
toast.setGravity(Gravity.CENTER, 0 , 0 ); |
16 |
toast
= Toast.makeText(getApplicationContext(), |
17 |
"带图片的Toast" ,
Toast.LENGTH_LONG); |
18 |
toast.setGravity(Gravity.CENTER, 0 , 0 ); |
19 |
LinearLayout
toastView = (LinearLayout) toast.getView(); |
20 |
ImageView
imageCodeProject = new ImageView(getApplicationContext()); |
21 |
imageCodeProject.setImageResource(R.drawable.icon); |
22 |
toastView.addView(imageCodeProject, 0 ); |
27 |
LayoutInflater
inflater = getLayoutInflater(); |
28 |
View
layout = inflater.inflate(R.layout.custom, |
29 |
(ViewGroup)
findViewById(R.id.llToast)); |
30 |
ImageView
image = (ImageView) layout |
31 |
.findViewById(R.id.tvImageToast); |
32 |
image.setImageResource(R.drawable.icon); |
33 |
TextView
title = (TextView) layout.findViewById(R.id.tvTitleToast); |
34 |
title.setText( "Attention" ); |
35 |
TextView
text = (TextView) layout.findViewById(R.id.tvTextToast); |
36 |
text.setText( "完全自定义Toast" ); |
37 |
toast
= new Toast(getApplicationContext()); |
38 |
toast.setGravity(Gravity.RIGHT
| Gravity.TOP, 12 , 40 ); |
39 |
toast.setDuration(Toast.LENGTH_LONG); |
40 |
toast.setView(layout); |
45 |
new Thread( new Runnable()
{ |