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

1、默认效果:

         view plain        copy        
  1. Toast toast = Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT); 
  2. toast.show(); 

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

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

3、带图片效果:

         view plain        copy        
  1. Toast toast = Toast.makeText(getApplicationContext(),"带图片的Toast", Toast.LENGTH_LONG); 
  2. toast.setGravity(Gravity.CENTER, 00); 
  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、完全自定义效果:

         view plain        copy        
  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.  
  10. Toast toast = new Toast(getApplicationContext()); 
  11. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 1240); 
  12. toast.setDuration(Toast.LENGTH_LONG); 
  13. toast.setView(layout); 
  14. toast.show(); 

5、其他线程:

         view plain        copy        
  1. public void login(){ 
  2.     new Thread(){ 
  3.         public void run(){ 
  4.             showToast(); 
  5.         } 
  6.     }.start(); 
  7.      
  8. //方法:在新的线程中使用Toast返回用户登录消息提示 
  9. Handler handler = new Handler(); 
  10. public void showToast() { 
  11.     handler.post(new Runnable() { 
  12.         @Override 
  13.         public void run() { 
  14.             Toast toast = Toast.makeText(LoginActivity.this"请输入账号或密码", Toast.LENGTH_LONG); 
  15.             toast.show(); 
  16.         } 
  17.     }); 
  18. }  



Android系统默认的Toast十分简洁,使用也非常的简单。但是有时我们的程序使用默认的Toast时会和程序的整体风格不搭配,这个时候我们就需要自定义Toast,使其与我们的程序更加融合。

使用自定义Toast,首先我们需要添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致,在该布局文件中我们需设计我们Toast的布局,例如:


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/toast_layout_root"  
  4.     android:orientation="horizontal"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:padding="10dp"  
  8.     android:background="#DAAA"  
  9.     >  
  10.     <ImageView android:id="@+id/image"  
  11.                android:layout_width="wrap_content"  
  12.                android:layout_height="fill_parent"  
  13.                android:layout_marginRight="10dp"  
  14.                />  
  15.     <TextView android:id="@+id/text"  
  16.               android:layout_width="wrap_content"  
  17.               android:layout_height="fill_parent"  
  18.               android:textColor="#FFF"  
  19.               />  
  20. LinearLayout>  

在这个地方要注意,我们给LinearLayout添加的id属性,在后面的代码中我们需要使用到。在程序中,我们可以通过如下代码创建我们自己的Toast。


  1. /** 
  2.  * @author coolszy 
  3.  * @blog http://blog.csdn.net/coolszy 
  4.  */  
  5.   
  6. public class MainActivity extends Activity  
  7. {  
  8.     private Button btn;  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState)  
  12.     {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.         btn = (Button) findViewById(R.id.btn);  
  16.         btn.setOnClickListener(new OnClickListener()  
  17.         {  
  18.             @Override  
  19.             public void onClick(View v)  
  20.             {  
  21.                 //获取LayoutInflater对象,该对象能把XML文件转换为与之一直的View对象  
  22.                 LayoutInflater inflater = getLayoutInflater();  
  23.                 //根据指定的布局文件创建一个具有层级关系的View对象  
  24.                 //第二个参数为View对象的根节点,即LinearLayout的ID  
  25.                 View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));  
  26.                   
  27.                 //查找ImageView控件  
  28.                 //注意是在layout中查找  
  29.                 ImageView image = (ImageView) layout.findViewById(R.id.image);  
  30.                 image.setImageResource(R.drawable.head);  
  31.                 TextView text = (TextView) layout.findViewById(R.id.text);  
  32.                 text.setText("自定义Toast演示程序");  
  33.   
  34.                 Toast toast = new Toast(getApplicationContext());  
  35.                 //设置Toast的位置  
  36.                 toast.setGravity(Gravity.CENTER_VERTICAL, 00);  
  37.                 toast.setDuration(Toast.LENGTH_LONG);  
  38.                 //让Toast显示为我们自定义的样子  
  39.                 toast.setView(layout);  
  40.                 toast.show();  
  41.             }  
  42.         });  
  43.     }  
  44. }  

运行效果:

 


更多相关文章

  1. Android(安卓)漂亮的 Dialog (弹出框)
  2. 漫谈android系统(1)解析android编译
  3. android中json解析的两个工具:Gson和Jackson的使用小demo
  4. Android之项目的目录结构
  5. android使用NDK编译curl库
  6. 自定义widgt and inflater
  7. Android(安卓)的Handle之postDelayed方法
  8. Android(安卓)软件测试日志文件抓取
  9. android文件下载!download!

随机推荐

  1. Android启动模式完全解析(下)
  2. Android调试的必杀技——反汇编
  3. AIDL使用详解及原理
  4. 关于在Mac上使用真机进行Android程序调试
  5. AndServer+Service打造Android服务器实现
  6. Android(安卓)GPS坐标 画路线
  7. Android(安卓)实现图片缓存异步加载框架
  8. Android(安卓)命名方式
  9. android studio下百度地图sdk的初体验
  10. Android的网络应用-使用HttpURLConnectio