Toast

Toast的基本原理其实就是将一个View添加到WindowManager中,让WindowManager来把View显示出来。(WindowManager可以将View显示在任何地方,任何Activity之上) 


Toast的默认属性  
Java代码   收藏代码
  1. // 对其方式为:水平居中,并在底部  
  2. mGravity = Gravtiy.CENTER_HORIZONTAL | Gravtiy.BOTTOM;  
  3.   
  4. mX = 0;  
  5. mY =context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);  
  6.   
  7. mHorizontalMargin = 0;  
  8. mVerticalMargin = 0;  
  9.   
  10. 所以用Toast.makeText(getApplicationContext(), R.string.text, Toast.LENGTH_SHORT).show();生成的Toast总是处在底部水平居中的位置  



在指定x, y处显示Toast  
Java代码   收藏代码
  1. // 在(50, 100)处显示Toast  
  2. Toast toast = Toast.makeText(getApplicationContext(), "toast use", Toast.LENGTH_SHORT);  
  3. toast.setGravity(Gravity.TOP | Gravity.LEFT, 50100);  
  4. toast.show();  
  5.   
  6. // 如果使用Gravity.NO_GRAVITY,后面的x, y就是相对于屏幕的中心点的(估计android是默认这么处理的)  
  7. Toast toast = Toast.makeText(getApplicationContext(), "toast use", Toast.LENGTH_SHORT);  
  8. toast.setGravity(Gravity.NO_GRAVITY, 50100);  
  9. toast.show();  
  10.   
  11. // 用margin来控制toast的位置  
  12. Toast toast = Toast.makeText(getApplicationContext(), "toast use", Toast.LENGTH_SHORT);  
  13. toast.setGravity(Gravity.LEFT | Gravity.TOP, 00);  
  14. // leftMargin, topMargin分别是容器width, height的%多少(这里是10%和20%)  
  15. toast.setMargin(0.1F, 0.2F);  
  16. toast.show();  



指定View的Toast  
Java代码   收藏代码
  1. // 布局xml:R.layout.toast  
  2. < Button  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@android:id/message"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content" />  
  7.   
  8.   
  9. Toast toast = new Toast(getApplicationContext());  
  10. toast.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.toast, null));  
  11. toast.setText("toast use");  
  12. // Button是否fill_parent是由gravity控制的, xml中的不起任何作用  
  13. toast.setGravity(toast.getGravity() | Gravity.FILL_HORIZONTAL,   
  14.     toast.getXOffset(), toast.getYOffset());  
  15. toast.setDuration(Toast.LENGTH_SHORT);  
  16. toast.show();  

Toast部分源码 
Java代码   收藏代码
  1. // Toast的构造器只设置了mY这个属性。mNextView, mDuration都没有设置(用makeText的话,这两个属性会设置)  
  2. public Toast(Context context) {  
  3.     mContext = context;  
  4.     mTN = new TN();  
  5.     mY = context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);  
  6. }  
  7.   
  8. // setText方法,需要将显示text的view的id设为@android:id/message,否则会抛RuntimeException  
  9. public void setText(CharSequence s) {  
  10.     if (mNextView == null) {  
  11.         throw new RuntimeException("This Toast was not created with Toast.makeText()");  
  12.     }  
  13.     TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);  
  14.     if (tv == null) {  
  15.         throw new RuntimeException("This Toast was not created with Toast.makeText()");  
  16.     }  
  17.     tv.setText(s);  
  18. }  




一直显示的Toast  
实现原理是:在Toast隐藏之前,再show一个相同的Toast,来实现长显示的假象 
Java代码   收藏代码
  1. private class ToastWrapper {  
  2.     private Toast mToast;  
  3.       
  4.     private Handler mHandler;  
  5.       
  6.     private Runnable mShowToast = new Runnable() {  
  7.         @Override  
  8.         public void run() {  
  9.             continueShow();  
  10.         }  
  11.     };  
  12.       
  13.     private boolean mCancelled = true;  
  14.       
  15.     public ToastWrapper(Context ctxt) {  
  16.         this(ctxt, new Handler());  
  17.     }  
  18.       
  19.     public ToastWrapper(Context ctxt, Handler handler) {  
  20.         mToast = Toast.makeText(ctxt, null, Toast.LENGTH_SHORT);  
  21.         mHandler = handler;  
  22.     }  
  23.       
  24.     public Toast getToast() {  
  25.         return mToast;  
  26.     }  
  27.       
  28.     public void showUntilCancel() {  
  29.         if (mCancelled) {  
  30.             mCancelled = false;  
  31.             mToast.setDuration(Toast.LENGTH_LONG);  
  32.             continueShow();  
  33.         }  
  34.     }  
  35.       
  36.     public void cancel() {  
  37.         mCancelled = true;  
  38.         mToast.cancel();  
  39.     }  
  40.       
  41.     private void continueShow() {  
  42.         if (mCancelled) {  
  43.             return;  
  44.         }  
  45.         mToast.show();  
  46.         mHandler.postDelayed(mShowToast, 3000);  
  47.     }  
  48. }  

使用ToastWrapper 
Java代码   收藏代码
  1. // 一直显示的toast  
  2. toastWrapper = new ToastWrapper(getApplicationContext());  
  3. Toast toast = toastWrapper.getToast();  
  4. toast.setText("toast wrapper");  
  5.   
  6. // ...  
  7.   
  8. Button button = new Button(getApplicationContext());  
  9. button.setText("一直显示toast");  
  10. button.setOnClickListener(new View.OnClickListener() {  
  11.     @Override  
  12.     public void onClick(View view) {  
  13.         toastWrapper.showUntilCancel();  
  14.     }  
  15. });  
  16.   
  17. Button button = new Button(getApplicationContext());  
  18. button.setText("隐藏toast");  
  19. button.setOnClickListener(new View.OnClickListener() {  
  20.     @Override  
  21.     public void onClick(View view) {  
  22.         toastWrapper.cancel();  
  23.     }  
  24. });  
  25.   
  26.   
  27.   
  28. // 一搬的toast  
  29. Button button = new Button(getApplicationContext());  
  30. button.setText("一般的toast");  
  31. button.setOnClickListener(new View.OnClickListener() {  
  32.     @Override  
  33.     public void onClick(View view) {  
  34.         Toast toast = toastWrapper.getToast();  
  35.         toast.setDuration(Toast.LENGTH_SHORT);  
  36.         toast.show();  
  37.     }  
  38. });  

Android 服务service里面出Toast:

  1. if(phoneIsInUse())  
  2.                 {  
  3.                     new Thread(new Runnable() {    
  4.                         public void run() {    
  5.                             Looper.prepare();    
  6.                             Toast  
  7.                             .makeText(  
  8.                                     VuiService.this,  
  9.                                     "请结束通话后再试", Toast.LENGTH_LONG).show();  
  10.                             Looper.loop();    
  11.                         }    
  12.                     }).start();    
  13.                     return ;  
  14.                 }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值