实现此功能没有太多的技术难点,主要通过PopupWindow方法,同时更进一步加深了PopupWindow的使用,实现点击弹出一个自定义的view,view里面可以自由设计,比较常用的可以放一个listview。
demo中我只是一个点击展示,简单的使用了fade in out的动画效果,也没有精美的图片资源,看着也丑,不过这么短的时间,让你掌握一个很好用的技术,可以自己扩展,不很好么?
废话不说了,直接上代码:
MainActivity.java
public class MainActivity extends Activity implements onClickListener {
private PopupWindow popupwindow;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setonClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (popupwindow != null&&popupwindow.isShowing()) {
popupwindow.dismiss();
return;
} else {
initmPopupWindowView();
popupwindow.showAsDropDown(v, 0, 5);
}
break;
default:
break;
}
}
public void initmPopupWindowView() {
// // 获取自定义布局文件pop.xml的视图
View customView = getLayoutInflater().inflate(R.layout.popview_item,
null, false);
// 创建PopupWindow实例,200,150分别是宽度和高度
popupwindow = new PopupWindow(customView, 250, 280);
// 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]
popupwindow.setAnimationStyle(R.style.AnimationFade);
// 自定义view添加触摸事件
customView.setonTouchListener(new onTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
popupwindow = null;
}
return false;
}
});
Button btton2 = (Button) customView.findViewById(R.id.button2);
Button btton3 = (Button) customView.findViewById(R.id.button3);
Button btton4 = (Button) customView.findViewById(R.id.button4);
btton2.setonClickListener(this);
btton3.setonClickListener(this);
btton4.setonClickListener(this);
}
}
activity_main.xml
自定义view的xml
动画效果:
inputodown.xml 进入屏幕
outdowntoup.xml
styles.xml
- @anim/inuptodown
- @anim/outdowntoup
实现效果:
以上所述就是本文对android使用PopupWindow实现页面点击顶部弹出下拉菜单的全部内容,希望大家喜欢。



