android Toast五种酷炫样式

在写Toast之前我们应该需要了解下 Toast是干什么的 或者说他是用来完成那些事情的,为了便于大家理解呢,我以我自己理解的程度来给大家举例子 Toast 英语是 “吐司”的意思,但是为什么要将它取这个名字我不是很理解,现在我说下我理解的toast 的用途,我们可以将 toast理解成为一种通知,也就是我们在操作 Android  之后 Android系统反馈给我们的信息,或者数据!

我们将toast理解为是一种通知,下面的样式我们就可以理解为不同形式的通知了,例如有的通知就是一张白纸然后上面的黑字,还有的或许就华丽些了,采用的是 彩纸 和 彩笔 后来更华丽了,那我估计就应该是 “请柬” 这种样子了!可以做到 图文并茂的效果了!

下面让我们一起来看看这些 “通知单”的效果!

 

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

1.默认效果

代码:

[java]  view plain  copy
 print ?
  1. Toast.makeText(getApplicationContext(), "默认Toast样式",   
  2. Toast.LENGTH_SHORT).show();  

 

2.自定义显示位置效果

 

代码

扫描二维码关注公众号,回复: 4866867 查看本文章
[java]  view plain  copy
 print ?
  1. toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);  
  2. toast.setGravity(Gravity.CENTER, 00);   
  3. toast.show();  


 

3.带图片效果

 

代码

[java]  view plain  copy
 print ?
  1. toast = Toast.makeText(getApplicationContext(),  
  2.      "带图片的Toast", Toast.LENGTH_LONG);  
  3.    toast.setGravity(Gravity.CENTER, 00);  
  4.    LinearLayout toastView = (LinearLayout) toast.getView();  
  5.    ImageView imageCodeProject = new ImageView(getApplicationContext());  
  6.    imageCodeProject.setImageResource(R.drawable.icon);  
  7.    toastView.addView(imageCodeProject, 0);  
  8.    toast.show();  


 

4.完全自定义效果

代码:

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


 

5.其他线程

代码

[java]  view plain  copy
 print ?
  1. new Thread(new Runnable() {  
  2.     public void run() {  
  3.      showToast();  
  4.     }  
  5.    }).start();  


 

 

完整代码

java  Activity 程序代码:

1.Main,java

[java]  view plain  copy
 print ?
  1. package com.wjq.toast;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class Main extends Activity implements OnClickListener {  
  17.  Handler handler = new Handler();  
  18.   
  19.  @Override  
  20.  public void onCreate(Bundle savedInstanceState) {  
  21.   super.onCreate(savedInstanceState);  
  22.   setContentView(R.layout.main);  
  23.   
  24.   findViewById(R.id.btnSimpleToast).setOnClickListener(this);  
  25.   findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(  
  26.     this);  
  27.   findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);  
  28.   findViewById(R.id.btnCustomToast).setOnClickListener(this);  
  29.   findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);  
  30.   
  31.  }  
  32.   
  33.  public void showToast() {  
  34.   handler.post(new Runnable() {  
  35.   
  36.    @Override  
  37.    public void run() {  
  38.     Toast.makeText(getApplicationContext(), "我来自其他线程!",  
  39.       Toast.LENGTH_SHORT).show();  
  40.   
  41.    }  
  42.   });  
  43.  }  
  44.   
  45.  @Override  
  46.  public void onClick(View v) {  
  47.   Toast toast = null;  
  48.   switch (v.getId()) {  
  49.   case R.id.btnSimpleToast:  
  50.    Toast.makeText(getApplicationContext(), "默认Toast样式",  
  51.      Toast.LENGTH_SHORT).show();  
  52.    break;  
  53.   case R.id.btnSimpleToastWithCustomPosition:  
  54.    toast = Toast.makeText(getApplicationContext(),  
  55.      "自定义位置Toast", Toast.LENGTH_LONG);  
  56.    toast.setGravity(Gravity.CENTER, 00);  
  57.    toast.show();  
  58.    break;  
  59.   case R.id.btnSimpleToastWithImage:  
  60.    toast = Toast.makeText(getApplicationContext(),  
  61.      "带图片的Toast", Toast.LENGTH_LONG);  
  62.    toast.setGravity(Gravity.CENTER, 00);  
  63.    LinearLayout toastView = (LinearLayout) toast.getView();  
  64.    ImageView imageCodeProject = new ImageView(getApplicationContext());  
  65.    imageCodeProject.setImageResource(R.drawable.icon);  
  66.    toastView.addView(imageCodeProject, 0);  
  67.    toast.show();  
  68.    break;  
  69.   case R.id.btnCustomToast:  
  70.    LayoutInflater inflater = getLayoutInflater();  
  71.    View layout = inflater.inflate(R.layout.custom,  
  72.      (ViewGroup) findViewById(R.id.llToast));  
  73.    ImageView image = (ImageView) layout  
  74.      .findViewById(R.id.tvImageToast);  
  75.    image.setImageResource(R.drawable.icon);  
  76.    TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
  77.    title.setText("Attention");  
  78.    TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
  79.    text.setText("完全自定义Toast");  
  80.    toast = new Toast(getApplicationContext());  
  81.    toast.setGravity(Gravity.RIGHT | Gravity.TOP, 1240);  
  82.    toast.setDuration(Toast.LENGTH_LONG);  
  83.    toast.setView(layout);  
  84.    toast.show();  
  85.    break;  
  86.   case R.id.btnRunToastFromOtherThread:  
  87.    new Thread(new Runnable() {  
  88.     public void run() {  
  89.      showToast();  
  90.     }  
  91.    }).start();  
  92.    break;  
  93.   
  94.   }  
  95.   
  96.  }  
  97. }  
  98.   
  99.    


布局文件

2.main,xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="vertical" android:layout_width="fill_parent"  
  4.  android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">  
  5.  <Button android:layout_height="wrap_content"  
  6.   android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"  
  7.   android:text="默认"></Button>  
  8.  <Button android:layout_height="wrap_content"  
  9.   android:layout_width="fill_parent" android:text="自定义显示位置"  
  10.   android:id="@+id/btnSimpleToastWithCustomPosition"></Button>  
  11.  <Button android:layout_height="wrap_content"  
  12.   android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"  
  13.   android:text="带图片"></Button>  
  14.  <Button android:layout_height="wrap_content"  
  15.   android:layout_width="fill_parent" android:text="完全自定义"  
  16.   android:id="@+id/btnCustomToast"></Button>  
  17.  <Button android:layout_height="wrap_content"  
  18.   android:layout_width="fill_parent" android:text="其他线程"  
  19.   android:id="@+id/btnRunToastFromOtherThread"></Button>  
  20.   
  21. </LinearLayout>  
  22.   
  23.    


布局文件

3.custom.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.  android:layout_height="wrap_content" android:layout_width="wrap_content"  
  5.  android:background="#ffffffff" android:orientation="vertical"  
  6.  android:id="@+id/llToast" >  
  7.  <TextView  
  8.   android:layout_height="wrap_content"  
  9.   android:layout_margin="1dip"  
  10.   android:textColor="#ffffffff"  
  11.   android:layout_width="fill_parent"  
  12.   android:gravity="center"  
  13.   android:background="#bb000000"  
  14.   android:id="@+id/tvTitleToast" />  
  15.  <LinearLayout  
  16.   android:layout_height="wrap_content"  
  17.   android:orientation="vertical"  
  18.   android:id="@+id/llToastContent"  
  19.   android:layout_marginLeft="1dip"  
  20.   android:layout_marginRight="1dip"  
  21.   android:layout_marginBottom="1dip"  
  22.   android:layout_width="wrap_content"  
  23.   android:padding="15dip"  
  24.   android:background="#44000000" >  
  25.   <ImageView  
  26.    android:layout_height="wrap_content"  
  27.    android:layout_gravity="center"  
  28.    android:layout_width="wrap_content"  
  29.    android:id="@+id/tvImageToast" />  
  30.   <TextView  
  31.    android:layout_height="wrap_content"  
  32.    android:paddingRight="10dip"  
  33.    android:paddingLeft="10dip"  
  34.    android:layout_width="wrap_content"  
  35.    android:gravity="center"  
  36.    android:textColor="#ff000000"  
  37.    android:id="@+id/tvTextToast" />  
  38.  </LinearLayout>  
  39. </LinearLayout>  
  40.   
  41.    

我们将toast理解为是一种通知,下面的样式我们就可以理解为不同形式的通知了,例如有的通知就是一张白纸然后上面的黑字,还有的或许就华丽些了,采用的是 彩纸 和 彩笔 后来更华丽了,那我估计就应该是 “请柬” 这种样子了!可以做到 图文并茂的效果了!

下面让我们一起来看看这些 “通知单”的效果!

 

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

1.默认效果

代码:

[java]  view plain  copy
 print ?
  1. Toast.makeText(getApplicationContext(), "默认Toast样式",   
  2. Toast.LENGTH_SHORT).show();  

 

2.自定义显示位置效果

 

代码

[java]  view plain  copy
 print ?
  1. toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);  
  2. toast.setGravity(Gravity.CENTER, 00);   
  3. toast.show();  


 

3.带图片效果

 

代码

[java]  view plain  copy
 print ?
  1. toast = Toast.makeText(getApplicationContext(),  
  2.      "带图片的Toast", Toast.LENGTH_LONG);  
  3.    toast.setGravity(Gravity.CENTER, 00);  
  4.    LinearLayout toastView = (LinearLayout) toast.getView();  
  5.    ImageView imageCodeProject = new ImageView(getApplicationContext());  
  6.    imageCodeProject.setImageResource(R.drawable.icon);  
  7.    toastView.addView(imageCodeProject, 0);  
  8.    toast.show();  


 

4.完全自定义效果

代码:

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


 

5.其他线程

代码

[java]  view plain  copy
 print ?
  1. new Thread(new Runnable() {  
  2.     public void run() {  
  3.      showToast();  
  4.     }  
  5.    }).start();  


 

 

完整代码

java  Activity 程序代码:

1.Main,java

[java]  view plain  copy
 print ?
  1. package com.wjq.toast;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class Main extends Activity implements OnClickListener {  
  17.  Handler handler = new Handler();  
  18.   
  19.  @Override  
  20.  public void onCreate(Bundle savedInstanceState) {  
  21.   super.onCreate(savedInstanceState);  
  22.   setContentView(R.layout.main);  
  23.   
  24.   findViewById(R.id.btnSimpleToast).setOnClickListener(this);  
  25.   findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(  
  26.     this);  
  27.   findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);  
  28.   findViewById(R.id.btnCustomToast).setOnClickListener(this);  
  29.   findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);  
  30.   
  31.  }  
  32.   
  33.  public void showToast() {  
  34.   handler.post(new Runnable() {  
  35.   
  36.    @Override  
  37.    public void run() {  
  38.     Toast.makeText(getApplicationContext(), "我来自其他线程!",  
  39.       Toast.LENGTH_SHORT).show();  
  40.   
  41.    }  
  42.   });  
  43.  }  
  44.   
  45.  @Override  
  46.  public void onClick(View v) {  
  47.   Toast toast = null;  
  48.   switch (v.getId()) {  
  49.   case R.id.btnSimpleToast:  
  50.    Toast.makeText(getApplicationContext(), "默认Toast样式",  
  51.      Toast.LENGTH_SHORT).show();  
  52.    break;  
  53.   case R.id.btnSimpleToastWithCustomPosition:  
  54.    toast = Toast.makeText(getApplicationContext(),  
  55.      "自定义位置Toast", Toast.LENGTH_LONG);  
  56.    toast.setGravity(Gravity.CENTER, 00);  
  57.    toast.show();  
  58.    break;  
  59.   case R.id.btnSimpleToastWithImage:  
  60.    toast = Toast.makeText(getApplicationContext(),  
  61.      "带图片的Toast", Toast.LENGTH_LONG);  
  62.    toast.setGravity(Gravity.CENTER, 00);  
  63.    LinearLayout toastView = (LinearLayout) toast.getView();  
  64.    ImageView imageCodeProject = new ImageView(getApplicationContext());  
  65.    imageCodeProject.setImageResource(R.drawable.icon);  
  66.    toastView.addView(imageCodeProject, 0);  
  67.    toast.show();  
  68.    break;  
  69.   case R.id.btnCustomToast:  
  70.    LayoutInflater inflater = getLayoutInflater();  
  71.    View layout = inflater.inflate(R.layout.custom,  
  72.      (ViewGroup) findViewById(R.id.llToast));  
  73.    ImageView image = (ImageView) layout  
  74.      .findViewById(R.id.tvImageToast);  
  75.    image.setImageResource(R.drawable.icon);  
  76.    TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
  77.    title.setText("Attention");  
  78.    TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
  79.    text.setText("完全自定义Toast");  
  80.    toast = new Toast(getApplicationContext());  
  81.    toast.setGravity(Gravity.RIGHT | Gravity.TOP, 1240);  
  82.    toast.setDuration(Toast.LENGTH_LONG);  
  83.    toast.setView(layout);  
  84.    toast.show();  
  85.    break;  
  86.   case R.id.btnRunToastFromOtherThread:  
  87.    new Thread(new Runnable() {  
  88.     public void run() {  
  89.      showToast();  
  90.     }  
  91.    }).start();  
  92.    break;  
  93.   
  94.   }  
  95.   
  96.  }  
  97. }  
  98.   
  99.    


布局文件

2.main,xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="vertical" android:layout_width="fill_parent"  
  4.  android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">  
  5.  <Button android:layout_height="wrap_content"  
  6.   android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"  
  7.   android:text="默认"></Button>  
  8.  <Button android:layout_height="wrap_content"  
  9.   android:layout_width="fill_parent" android:text="自定义显示位置"  
  10.   android:id="@+id/btnSimpleToastWithCustomPosition"></Button>  
  11.  <Button android:layout_height="wrap_content"  
  12.   android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"  
  13.   android:text="带图片"></Button>  
  14.  <Button android:layout_height="wrap_content"  
  15.   android:layout_width="fill_parent" android:text="完全自定义"  
  16.   android:id="@+id/btnCustomToast"></Button>  
  17.  <Button android:layout_height="wrap_content"  
  18.   android:layout_width="fill_parent" android:text="其他线程"  
  19.   android:id="@+id/btnRunToastFromOtherThread"></Button>  
  20.   
  21. </LinearLayout>  
  22.   
  23.    


布局文件

3.custom.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.  android:layout_height="wrap_content" android:layout_width="wrap_content"  
  5.  android:background="#ffffffff" android:orientation="vertical"  
  6.  android:id="@+id/llToast" >  
  7.  <TextView  
  8.   android:layout_height="wrap_content"  
  9.   android:layout_margin="1dip"  
  10.   android:textColor="#ffffffff"  
  11.   android:layout_width="fill_parent"  
  12.   android:gravity="center"  
  13.   android:background="#bb000000"  
  14.   android:id="@+id/tvTitleToast" />  
  15.  <LinearLayout  
  16.   android:layout_height="wrap_content"  
  17.   android:orientation="vertical"  
  18.   android:id="@+id/llToastContent"  
  19.   android:layout_marginLeft="1dip"  
  20.   android:layout_marginRight="1dip"  
  21.   android:layout_marginBottom="1dip"  
  22.   android:layout_width="wrap_content"  
  23.   android:padding="15dip"  
  24.   android:background="#44000000" >  
  25.   <ImageView  
  26.    android:layout_height="wrap_content"  
  27.    android:layout_gravity="center"  
  28.    android:layout_width="wrap_content"  
  29.    android:id="@+id/tvImageToast" />  
  30.   <TextView  
  31.    android:layout_height="wrap_content"  
  32.    android:paddingRight="10dip"  
  33.    android:paddingLeft="10dip"  
  34.    android:layout_width="wrap_content"  
  35.    android:gravity="center"  
  36.    android:textColor="#ff000000"  
  37.    android:id="@+id/tvTextToast" />  
  38.  </LinearLayout>  
  39. </LinearLayout>  
  40.   
  41.    

猜你喜欢

转载自blog.csdn.net/SIMPLE1995/article/details/53012751