Toast的基本原理其實就是將一個View添加到WindowManager中,讓WindowManager來把View顯示出來。(WindowManager可以將View顯示在任何地方,任何Activity之上)
Toast的默認屬性
在指定x, y處顯示Toast
指定View的Toast
Toast部分源碼
一直顯示的Toast
實現原理是:在Toast隱藏之前,再show一個相同的Toast,來實現長顯示的假象
使用ToastWrapper
Toast的默認屬性
- //對其方式為:水平居中,并在底部
- mGravity=Gravtiy.CENTER_HORIZONTAL|Gravtiy.BOTTOM;
- mX= 0 ;
- mY=context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);
- mHorizontalMargin= 0 ;
- mVerticalMargin= 0 ;
- 所以用Toast.makeText(getApplicationContext(),R.string.text,Toast.LENGTH_SHORT).show();生成的Toast總是處在底部水平居中的位置
在指定x, y處顯示Toast
- //在(50,100)處顯示Toast
- Toasttoast=Toast.makeText(getApplicationContext(), "toastuse" ,Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.TOP|Gravity.LEFT, 50 , 100 );
- toast.show();
- //如果使用Gravity.NO_GRAVITY,后面的x,y就是相對于屏幕的中心點的(估計android是默認這么處理的)
- Toasttoast=Toast.makeText(getApplicationContext(), "toastuse" ,Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.NO_GRAVITY, 50 , 100 );
- toast.show();
- //用margin來控制toast的位置
- Toasttoast=Toast.makeText(getApplicationContext(), "toastuse" ,Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.LEFT|Gravity.TOP, 0 , 0 );
- //leftMargin,topMargin分別是容器width,height的%多少(這里是10%和20%)
- toast.setMargin( 0 .1F, 0 .2F);
- toast.show();
指定View的Toast
- //布局xml:R.layout.toast
- <Button
- xmlns:android= "http://schemas.android.com/apk/res/android"
- android:id= "@android:id/message"
- android:layout_width= "fill_parent"
- android:layout_height= "wrap_content" />
- Toasttoast= new Toast(getApplicationContext());
- toast.setView(LayoutInflater.from(getApplicationContext()).inflate(R.layout.toast, null ));
- toast.setText( "toastuse" );
- //Button是否fill_parent是由gravity控制的,xml中的不起任何作用
- toast.setGravity(toast.getGravity()|Gravity.FILL_HORIZONTAL,
- toast.getXOffset(),toast.getYOffset());
- toast.setDuration(Toast.LENGTH_SHORT);
- toast.show();
Toast部分源碼
- //Toast的構造器只設置了mY這個屬性。mNextView,mDuration都沒有設置(用makeText的話,這兩個屬性會設置)
- public Toast(Contextcontext){
- mContext=context;
- mTN= new TN();
- mY=context.getResources().getDimensionPixelSize(com.android.internal.R.dimen.toast_y_offset);
- }
- //setText方法,需要將顯示text的view的id設為@android:id/message,否則會拋RuntimeException
- public void setText(CharSequences){
- if (mNextView== null ){
- throw new RuntimeException( "ThisToastwasnotcreatedwithToast.makeText()" );
- }
- TextViewtv=(TextView)mNextView.findViewById(com.android.internal.R.id.message);
- if (tv== null ){
- throw new RuntimeException( "ThisToastwasnotcreatedwithToast.makeText()" );
- }
- tv.setText(s);
- }
一直顯示的Toast
實現原理是:在Toast隱藏之前,再show一個相同的Toast,來實現長顯示的假象
- private class ToastWrapper{
- private ToastmToast;
- private HandlermHandler;
- private RunnablemShowToast= new Runnable(){
- @Override
- public void run(){
- continueShow();
- }
- };
- private boolean mCancelled= true ;
- public ToastWrapper(Contextctxt){
- this (ctxt, new Handler());
- }
- public ToastWrapper(Contextctxt,Handlerhandler){
- mToast=Toast.makeText(ctxt, null ,Toast.LENGTH_SHORT);
- mHandler=handler;
- }
- public ToastgetToast(){
- return mToast;
- }
- public void showUntilCancel(){
- if (mCancelled){
- mCancelled= false ;
- mToast.setDuration(Toast.LENGTH_LONG);
- continueShow();
- }
- }
- public void cancel(){
- mCancelled= true ;
- mToast.cancel();
- }
- private void continueShow(){
- if (mCancelled){
- return ;
- }
- mToast.show();
- mHandler.postDelayed(mShowToast, 3000 );
- }
- }
使用ToastWrapper
- //一直顯示的toast
- toastWrapper= new ToastWrapper(getApplicationContext());
- Toasttoast=toastWrapper.getToast();
- toast.setText( "toastwrapper" );
- //...
- Buttonbutton= new Button(getApplicationContext());
- button.setText( "一直顯示toast" );
- button.setOnClickListener( new View.OnClickListener(){
- @Override
- public void onClick(Viewview){
- toastWrapper.showUntilCancel();
- }
- });
- Buttonbutton= new Button(getApplicationContext());
- button.setText( "隱藏toast" );
- button.setOnClickListener( new View.OnClickListener(){
- @Override
- public void onClick(Viewview){
- toastWrapper.cancel();
- }
- });
- //一搬的toast
- Buttonbutton= new Button(getApplicationContext());
- button.setText( "一般的toast" );
- button.setOnClickListener( new View.OnClickListener(){
- @Override
- public void onClick(Viewview){
- Toasttoast=toastWrapper.getToast();
- toast.setDuration(Toast.LENGTH_SHORT);
- toast.show();
- }
- });
Android 服務service里面出Toast:
- if (phoneIsInUse())
- {
- new Thread( new Runnable(){
- public void run(){
- Looper.prepare();
- Toast
- .makeText(
- VuiService. this ,
- "請結束通話后再試" ,Toast.LENGTH_LONG).show();
- Looper.loop();
- }
- }).start();
- return ;
- }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
