Android高手进阶教程(十)之----Android PopupWindow的使用!!!

大家好,我们这一节讲的是Android PopupWindow的使用! 在我理解其实PopupWindow其实类似于一个不能动的Widget(仅从显示效果来说!)

它是浮在别的窗口之上的.

下面我将给大家做一个简单的Demo,类似于音乐播放器的Widget的效果,点击Button的时候出来PopupWindow,首先我们看一下效果图:

下面是核心代码:

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


需要强调的是这里PopupWindow必须有某个事件触发才会显示出来,不然总会抱错,不信大家可以试试!

随着这个问题的出现,就会同学问了,那么我想初始化让PopupWindow显示出来,那怎么办了,不去寄托于其他点击事件,

在这里我用了定时器Timer来实现这样的效果,当然这里就要用到Handler了,如果大家不理解的可以返回

Android 高手进阶教程(九)之----Android Handler的使用!! 看一看,加深了解:

下面是核心代码:

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

效果如下图:

这样就可以初始化PopupWindow了,呵呵,这一节的布局文件有点多,如果大家想要源码的话,留下你们的Email,我会尽快发送给大家的

,今天就到这里,大家有什么不明白的欢迎留言!!!谢谢~

要源码的太多,我快崩溃了,所以上传了。下载地址: http://d.download.csdn.net/down/2871531/Android_Tutor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值