Android之Toast的用法


有些程序可以配置,用来让用户设置有些自定义的偏好

Toast是用来向用户展示一些提示性信息,并且该VIEW永远不会获得系统的焦点

一般定义为:Toast.makeText(MyClass.this,"note info",Toast.LENGTH_SHORT).show();

也可以在Tost中显示出自定义的VIEW,如下所示展示出图片和文字

自定义的 一个显示Toast的方法

  1. protected void showToast() {  
  2.     // create the view   
  3.     View view = inflateView(R.layout.incoming_message_panel);  
  4.   
  5.     // set the text in the view   
  6.     TextView tv = (TextView)view.findViewById(R.id.message);  
  7.     tv.setText("khtx. meet u for dinner. cul8r");  
  8.   
  9.     // show the toast   
  10.     Toast toast = new Toast(this);  
  11.     toast.setView(view);  
  12.     toast.setDuration(Toast.LENGTH_LONG);  
  13.     toast.show();  
  14. }  

incoming_message_panel的定义如下所示:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@android:drawable/toast_frame">  
  6.   
  7.     <LinearLayout  
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content">  
  11.   
  12.             <ImageView  
  13.                 android:layout_width="wrap_content"  
  14.                 android:layout_height="wrap_content"  
  15.                 android:src="@drawable/sample_thumb_2"  
  16.                 />  
  17.   
  18.             <TextView  
  19.                 android:id="@+id/message"  
  20.                 android:layout_gravity="center_vertical"  
  21.                 android:layout_width="wrap_content"  
  22.                 android:layout_height="wrap_content"  
  23.                 android:paddingLeft="6dip"  
  24.                 />  
  25.   
  26.     </LinearLayout>  
  27. </FrameLayout>  

toast_frame图片如下所示


相关内容