这一节讲的是Android PopupWindow的使用! 在我理解其实PopupWindow其实类似于一个不能动的Widget(仅从显示效果来说!)它是浮在别的窗口之上的.下面我将给大家做一个简单的Demo,类似于音乐播放器的Widget的效果,点击Button的时候出来PopupWindow,首先我们看一下效果图:
下载 (9.2 KB)
2010-7-27 17:34
下载 (10.47 KB)
2010-7-27 17:34
下面是核心代码:
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显示出来,那怎么办了,不去寄托于其他点击事件,在这里我用了定时器Time来实现这样的效果,当然这里就要用到Handler了下面是核心代码:
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;
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);
}
}
下载 (10.5 KB)
2010-7-27 17:39
转自:http://blog.csdn.net/Android_Tutor/archive/2010/05/10/5576533.aspx
分享到:
相关推荐
Android PopupWindow使用示例,更多信息请访问http://blog.csdn.net/qingdujun/article/details/42609549
android中popupWindow使用案列。点击按钮,显示出菜单按钮。
PopupWindow的简单使用!
android popupwindow 底部灰色背景
android PopupWindow的使用(带动画效果)源码
Android PopupWindow实现右侧、左侧和底部弹出菜单 效果图:http://blog.csdn.net/jianfengwen/article/details/48629217
主要介绍了Android PopupWindow使用实例,本文直接给出代码实例,需要的朋友可以参考下
Android PopupWindow与Activity之间的数据传递
Android popupwindow 实例及使用
Android从屏幕底部滑动弹出PopupWindow,有动画效果,类似于sharesdk的分享页面滑动弹出效果。
Android PopupWindow显示在控件上方或者下方,完整的例子下载分享给有需要的人;内部填充数据用的自定义LinearLayout,可以动态添加标题,在Activity中处理回调方法.项目中摘取,有些冗余代码,亲要自己简单处理下哦...
安卓中的弹窗的Demo,有几种不同类型的弹窗PopupWindow
Android PopupWindow泡泡效果(有listview)
主要为大家详细介绍了Android PopupWindow使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在android 开发中,让popupwindow 弹出在点击按钮的上方中间; 说明文档见我的博客
Android PopupWindow的使用详解 http://blog.csdn.net/yalinfendou博客中有详细介绍……
在PopupWindow上实现图片的缩放
经过多番测试实践,实现了popupwindow 弹出在指定控件的下方。代码上有注释,有需要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。
android 封装一个通用PopupWindow,避免重复造轮子,简化代码,方便管理。
android通过使用popupwindow控件实现自定义menu菜单