大家好,我們這一節講的是Android PopupWindow的使用! 在我理解其實PopupWindow其實類似於一個不能動的Widget(僅從顯示效果來說!)
它是浮在別的視窗之上的.
下面我將給大家做一個簡單的Demo,類似於音樂播放器的Widget的效果,點擊Button的時候出來PopupWindow,首先我們看一下:
下面是核心代碼:
view plaincopy to clipboardprint?
- 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);
- }
- }
- }
package com.android.tutor;<br />import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.Gravity;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.Button;<br />import android.widget.PopupWindow;<br />public class PopupWindowDemo extends Activity implements OnClickListener{<br /> private Button btn; </p><p> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); </p><p> btn = (Button)findViewById(R.id.btn);<br /> btn.setOnClickListener(this);<br /> }<br /> @Override<br /> public void onClick(View v) {<br /> Context mContext = PopupWindowDemo.this;<br /> if (v.getId() == R.id.btn) {<br /> LayoutInflater mLayoutInflater = (LayoutInflater) mContext<br /> .getSystemService(LAYOUT_INFLATER_SERVICE);<br /> View music_popunwindwow = mLayoutInflater.inflate(<br /> R.layout.music_popwindow, null);<br /> PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, LayoutParams.FILL_PARENT,<br /> LayoutParams.WRAP_CONTENT); </p><p> mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.BOTTOM, 0, 0);<br /> }<br /> }<br />}
需要強調的是這裡PopupWindow必須有某個事件觸發才會顯示出來,不然總會抱錯,不信大家可以試試!
隨著這個問題的出現,就會同學問了,那麼我想初始化讓PopupWindow顯示出來,那怎麼辦了,不去寄託於其他點擊事件,
在這裡我用了定時器Timer來實現這樣的效果,當然這裡就要用到Handler了,如果大家不理解的可以返回
view plaincopy to clipboardprint?
- Android 高手進階教程(九)之----Android Handler的使用!! 看一看,加深瞭解:
Android 高手進階教程(九)之----Android Handler的使用!! 看一看,加深瞭解:
下面是核心代碼:
view plaincopy to clipboardprint?
- 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);
- }
- }
package com.android.tutor;<br />import java.util.Timer;<br />import java.util.TimerTask;<br />import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.os.Handler;<br />import android.os.Message;<br />import android.view.Gravity;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.PopupWindow;<br />public class PopupWindowDemo extends Activity{<br /> private Handler mHandler = new Handler(){ </p><p> public void handleMessage(Message msg) {<br /> switch (msg.what) {<br /> case 1:<br /> showPopupWindow();<br /> break;<br /> }<br /> };<br /> }; </p><p> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main); </p><p> //create the timer<br /> Timer timer = new Timer();<br /> timer.schedule(new initPopupWindow(), 100);<br /> } </p><p> private class initPopupWindow extends TimerTask{<br /> @Override<br /> public void run() { </p><p> Message message = new Message();<br /> message.what = 1;<br /> mHandler.sendMessage(message); </p><p> }<br /> } </p><p> public void showPopupWindow() {<br /> Context mContext = PopupWindowDemo.this;<br /> LayoutInflater mLayoutInflater = (LayoutInflater) mContext<br /> .getSystemService(LAYOUT_INFLATER_SERVICE);<br /> View music_popunwindwow = mLayoutInflater.inflate(<br /> R.layout.music_popwindow, null);<br /> PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow,<br /> LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);<br /> mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);<br /> }<br />}
效果如:
這樣就可以初始化PopupWindow了,呵呵,這一節的布局檔案有點多,如果大家想要源碼的話,留下你們的Email,我會儘快發送給大家的,今天就到這裡,大家有什麼不明白的歡迎留言!!!謝謝~