[代码] [Java]代码
01 | 1 .默认效果: |
02 |
03 | 代码: |
04 | Toast.makeText(getApplicationContext(), "默认Toast样式" , |
05 | Toast.LENGTH_SHORT).show(); |
06 |
07 | 2 .自定义显示位置效果: |
08 |
09 | 代码: |
10 | toast = Toast.makeText(getApplicationContext(), |
11 | "自定义位置Toast" , Toast.LENGTH_LONG); |
12 | toast.setGravity(Gravity.CENTER, 0 , 0 ); |
13 | toast.show(); |
14 | 3 .带图片效果: |
15 | 代码 |
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 ); |
23 | toast.show |
24 | 4 .完全自定义效果: |
25 |
26 | 代码 |
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); |
41 | toast.show(); |
42 | 5 .其他线程: |
43 |
44 | 代码: |
45 | new Thread( new Runnable() { |
46 | public void run() { |
47 | showToast(); |
48 | } |
49 | }).start(); |