Android PopupWindow 的使用

來源:互聯網
上載者:User

標籤:android   style   http   color   io   os   使用   java   ar   

在我理解其實PopupWindow其實類似於一個不能動的Widget(僅從顯示效果來說!)。它是浮在別的視窗之上的.

 

下面我將給大家做一個簡單的Demo,類似於音樂播放器的Widget的效果,點擊Button的時候出來PopupWindow,首先我們看一下:


下面是核心代碼:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.android.tutor;  import android.app.Activity;  import android.content.Context;  import android.os.Bundle;  import android.view.Gravity;  import android.view.LayoutInflater;  import android.view.View;  import android.view.View.OnClickListener;  import android.view.ViewGroup.LayoutParams;  import android.widget.Button;  import android.widget.PopupWindow;  public class PopupWindowDemo extends Activity  implements OnClickListener{       private Button btn;               public void onCreate(Bundle savedInstanceState) {           super .onCreate(savedInstanceState);           setContentView(R.layout.main);                       btn = (Button)findViewById(R.id.btn);           btn.setOnClickListener( this );       }       @Override       public void onClick(View v) {           Context mContext = PopupWindowDemo. this ;           if (v.getId() == R.id.btn) {               LayoutInflater mLayoutInflater = (LayoutInflater) mContext                       .getSystemService(LAYOUT_INFLATER_SERVICE);               View music_popunwindwow = mLayoutInflater.inflate(                       R.layout.music_popwindow, null );               PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT,                       LayoutParams.WRAP_CONTENT);                               mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.BOTTOM, 0 , 0 );           }       }  }

需要強調的是這裡PopupWindow必須有某個事件觸發才會顯示出來,不然總會抱錯,不信大家可以試試! 


隨著這個問題的出現,就會同學問了,那麼我想初始化讓PopupWindow顯示出來,那怎麼辦了,不去寄託於其他點擊事件。在這裡我用了定時器Timer來實現這樣的效果,當然這裡就要用到Handler了:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package com.android.tutor;  import java.util.Timer;  import java.util.TimerTask;  import android.app.Activity;  import android.content.Context;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.view.Gravity;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup.LayoutParams;  import android.widget.PopupWindow;  public class PopupWindowDemo extends Activity{       private Handler mHandler = new Handler(){                       public void handleMessage(Message msg) {               switch (msg.what) {               case 1 :                   showPopupWindow();                   break ;               }           };       };               public void onCreate(Bundle savedInstanceState) {           super .onCreate(savedInstanceState);           setContentView(R.layout.main);                       //create the timer             Timer timer = new Timer();           timer.schedule( new initPopupWindow(), 100 );       }               private class initPopupWindow extends TimerTask{           @Override           public void run() {                               Message message = new Message();               message.what = 1 ;               mHandler.sendMessage(message);                           }              }                       public void showPopupWindow() {           Context mContext = PopupWindowDemo. this ;           LayoutInflater mLayoutInflater = (LayoutInflater) mContext                   .getSystemService(LAYOUT_INFLATER_SERVICE);           View music_popunwindwow = mLayoutInflater.inflate(                   R.layout.music_popwindow, null );           PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,                   LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);           mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0 , 0 );       }  }

效果如:

 

Android PopupWindow 的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.