《Andrioid_Popupwindow案例》popWindow做Menu,popWindow做选项菜单
2020-11-24 01:08
标签:popupwindow做菜单 popupwindow做选项 popupwindow做选择列表 先看效果,上图展示了最近在开发的项目经常用到的pop案例,包括一般的选项,下拉选项,menu等,布局就不贴了,很简单 相信都能秒懂,贴下pop的核心代码 上面就是menupop的源代码。能实现pop按Menu键可以弹出,再次menu键消失,点击其他区域,或按下返回键,或者pop的view有点击事件的时候pop消失。这里具体借鉴了http://blog.csdn.net/admin_/article/details/7278402 这篇文章,这篇文章写的不错。刚开始也遇到了pop弹出来不消失的情况, setFocusable(true); popView.setFocusableInTouchMode(true); 这两个方法很关键。pop在弹出的之后就变成了僵尸View,但是Pop里面的View还是可以接收事件的,但是这时候要先把焦点传给pop的view,这样View是可以响应onkeydown事件和onClick事件的。问题得到解决。 来看下MainActivity 《Andrioid_Popupwindow案例》popWindow做Menu,popWindow做选项菜单,搜素材,soscw.com 《Andrioid_Popupwindow案例》popWindow做Menu,popWindow做选项菜单 标签:popupwindow做菜单 popupwindow做选项 popupwindow做选择列表 原文地址:http://blog.csdn.net/wangyg1990/article/details/25318609/**
*
* @author yaguang.wang
*
*/
public class MenuPop extends PopupWindow {
private int resId;
private Context mContext;
private View popView;
private LayoutInflater inflater;
public MenuPop(int resId,Context context) {
super(context);
this.resId = resId;
mContext = context;
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
init();
}
@SuppressWarnings("deprecation")
public void init(){
popView = inflater.inflate(this.resId, null);
popView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
setContentView(popView);
setWidth(LayoutParams.FILL_PARENT);
setHeight(LayoutParams.WRAP_CONTENT);
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setFocusable(true);
Button btn1 = (Button) popView.findViewById(R.id.menu_btn1);
Button btn2 = (Button) popView.findViewById(R.id.menu_btn2);
Button btn3 = (Button) popView.findViewById(R.id.menu_btn3);
Button btn4 = (Button) popView.findViewById(R.id.menu_btn4);
btn1.setOnClickListener(new ButtonListener());
btn2.setOnClickListener(new ButtonListener());
btn3.setOnClickListener(new ButtonListener());
btn4.setOnClickListener(new ButtonListener());
popView.setFocusableInTouchMode(true);
popView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU && isShowing()){
dismiss();
return true;
}
return false;
}
});
}
private class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
Toast.makeText(mContext, "view id is"+v.getId(), Toast.LENGTH_SHORT).show();
if (isShowing()) {
dismiss();
}
}
}
}
/**
*
* @author yaguang.wang
*
*/
public class GridPop extends PopupWindow {
private Context context;
private LayoutInflater layoutInflater;
public View allView;
private int resId;
private GridView allItemGrid;
public GridPop(Context context,int resourceId) {
super(context);
this.context = context;
this.resId=resourceId;
initAllPop();
}
public void initAllPop() {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
allView = layoutInflater.inflate(resId, null);
allView.setFocusable(true);
allView.setFocusableInTouchMode(true);
allItemGrid = (GridView) allView.findViewById(R.id.pop_grid);
allView.setFocusableInTouchMode(true);
allView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (isShowing()) {
dismiss();
return true;
}
return false;
}
});
DisplayMetrics dm=new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
setContentView(allView);
setWidth(dm.widthPixels/2);
setHeight(LayoutParams.WRAP_CONTENT);
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setTouchable(true);
setFocusable(true);
setOutsideTouchable(true);
}
/**
*
* @return 返回该pop中GridView,可以其他地方设置该GridView
*/
public GridView getAllItemGrid() {
return allItemGrid;
}
}
上面是gridpop的代码。里面是gridview,其实pop里面可以填很多View的。不再赘述,在封装pop之后,只需要改下布局文件,改下数据,在其他地方就可以方便的new出来不同需求的pop。比如在基类activity做了popmenu的popwindow的操作,那么其他子activity就可以很方便的继承。/**
*
* @author yaguang.wang
*
*/
public class Activity_Main extends Activity {
private Button btn1,btn2;
private MenuPop popMenu;
private DefaultPop defaultPop;
private GridPop gridpop;
private ArrayList
其他部分的代码就不贴出了。共同学习,源码,在http://download.csdn.net/detail/wangyg1990/7317203
文章标题:《Andrioid_Popupwindow案例》popWindow做Menu,popWindow做选项菜单
文章链接:http://soscw.com/index.php/essay/22292.html