`

Android软件开发之 自定义别样Toast

 
阅读更多

转于:http://blog.csdn.net/jackhenry/article/details/7340278

------------------------------------------------------------------------------------

 

 在Android开发过程中,相信大家对Toast都很熟悉,在一定程度上它可以提高用户体验,比如用户登录浏览器时,浏览器肯定会检测当前设备的网络连接状况,如果当前设备的网络连接是断开的,就可以使用Toast告诉用户,网络初始化失败!让用户检测是否打开数据开关或连接Wifi,如图:

 

但是这种样式的显示有时候不能满足我们的需求,而且看起来也比较不美观。

然后本人就想重写写个不一样的Toast,再查看了Android的源码后,发现非常简单。

Android源码Toast.java部分代码:

[java] view plaincopy
 
  1. public static Toast makeText(Context context, CharSequence text, int duration) {  
  2.     Toast result = new Toast(context);  
  3.       LayoutInflater inflate = (LayoutInflater)  
  4.              context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  5.       View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);  
  6.       TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);  
  7.       tv.setText(text);  
  8.   
  9.          
  10.       result.mNextView = v;  
  11.       result.mDuration = duration;  
  12.   
  13.       return result;  
  14. }  

 

-----------------------------------------^-^......直接上代码

新的Toast的layout文件 newtoast.xml:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:gravity="center_vertical"  
  6.   android:background="@drawable/btn_bg"  
  7.   android:layout_width="fill_parent"  
  8.   android:layout_height="fill_parent">  
  9.   <ImageView   
  10.     android:id="@+id/image0"  
  11.     android:layout_width="wrap_content"  
  12.     android:layout_height="wrap_content"  
  13.     android:layout_gravity="center_horizontal"  
  14.     />  
  15.   <TextView   
  16.     android:id="@+id/text0"  
  17.     android:layout_width="wrap_content"  
  18.     android:layout_height="wrap_content"  
  19.     android:layout_gravity="center_horizontal"  
  20.     android:textSize="10sp"  
  21.     android:textColor="#000"  
  22.     />  
  23.       
  24. </LinearLayout>  

 

新Toast背景 btn_bg.xml:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <corners   
  5.         android:radius="6dp"  
  6.         />  
  7.     <padding   
  8.         android:left="5dp"  
  9.         android:top="5dp"  
  10.         android:right="5dp"  
  11.         android:bottom="5dp"  
  12.         />  
  13.     <solid   
  14.         android:color="#FFFFFF"  
  15.         />  
  16. </shape>  

 

NewToast.java:

[java] view plaincopy
 
  1. public class NewToast extends Toast {  
  2.   
  3.     public NewToast(Context context) {  
  4.         super(context);  
  5.     }  
  6.       
  7.     public static Toast makeText(Context context, int resId, CharSequence text, int duration) {  
  8.         Toast result = new Toast(context);  
  9.           
  10.         //获取LayoutInflater对象  
  11.         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
  12.         //由layout文件创建一个View对象  
  13.         View layout = inflater.inflate(R.layout.newtoast, null);  
  14.           
  15.         //实例化ImageView和TextView对象  
  16.         ImageView imageView = (ImageView) layout.findViewById(R.id.image0);  
  17.         TextView textView = (TextView) layout.findViewById(R.id.text0);  
  18.           
  19.         imageView.setImageResource(resId);  
  20.         textView.setText(text);  
  21.           
  22.         result.setView(layout);  
  23.         result.setGravity(Gravity.CENTER_VERTICAL, 00);  
  24.         result.setDuration(duration);  
  25.           
  26.         return result;  
  27.     }  
  28.   
  29. }  


main.xml:

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <Button    
  8.     android:id="@+id/btn0"  
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="@string/ic_btn_name"  
  12.     android:background="@drawable/btn_bg"  
  13.     />  
  14. </LinearLayout>  


strings.xml:

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">NewToastDemo</string>  
  4.     <string name="ic_btn_name">测试按钮</string>  
  5. </resources>  

最后来测试一下,MainActivity.java:

[java] view plaincopy
 
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.       
  3.     //声明一个Button对象  
  4.     private Button btn;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         btn = (Button) findViewById(R.id.btn0);  
  11.         btn.setOnClickListener(this);  
  12.     }  
  13.     @Override  
  14.     public void onClick(View v) {  
  15.         NewToast.makeText(MainActivity.this, R.drawable.prettygirl, "这是一个自定义的Toast!", Toast.LENGTH_SHORT).show();  
  16.     }  
  17. }  


run app之后,点击按钮,出现如下图所示:

这样一个新的Toast就写好了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics