一个测试ok的popupwindow demo
16lz
2022-04-21
/** * 弹出PopupWindow */public void initPopupWindow() {// 加载PopupWindow的布局文件LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);View view = layoutInflater.inflate(R.layout.popup, null);// 加载PopupWindow的媒介布局文件View parentView = layoutInflater.inflate(R.layout.first, null);// 实例化PopupWindow中的组件// TextView titleTextView = (TextView)// view.findViewById(R.id.popup_title);// ImageView editImg = (ImageView) view.findViewById(R.id.popup_edit);// ImageView listenImg = (ImageView)// view.findViewById(R.id.popup_listen);// SeekBar seekBar = (SeekBar) view.findViewById(R.id.popup_progress);// TextView progressTextView = (TextView) view// .findViewById(R.id.popup_progress_tv);// view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,// LayoutParams.WRAP_CONTENT));TextView black=(TextView)view.findViewById(R.id.black_txt);black.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (popupWindow != null) {popupWindow.dismiss();}}});GridView gridView = (GridView) view.findViewById(R.id.popup_grid);GridViewAdapter adapter = new GridViewAdapter(this, mPictures, mTitles);gridView.setAdapter(adapter);gridView.setOnItemClickListener(new ItemClickListener());gridView.setOnItemSelectedListener(new OnItemSelectedListener() {/* * (non-Javadoc) * * @see * android.widget.AdapterView.OnItemSelectedListener#onItemSelected * (android.widget.AdapterView, android.view.View, int, long) */@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stub}/* * (non-Javadoc) * * @see * android.widget.AdapterView.OnItemSelectedListener#onNothingSelected * (android.widget.AdapterView) */@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});// 声明并实例化PopupWindowpopupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);// 为PopupWindow设置弹出的位置// popupWindow.showAsDropDown(parentView);// popupWindow.setBackgroundDrawable(new BitmapDrawable());popupWindow.setFocusable(true);popupWindow.setTouchable(true);// popupWindow.// popupWindow.update();// popupWindow.setBackgroundDrawable(new BitmapDrawable());popupWindow.setOutsideTouchable(true);// 点击窗口外消失,需要设置背景、焦点、touchable、update// popupWindow.setpopupWindow.setWindowLayoutMode(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);popupWindow.showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0);// popupWindow.setFocusable(true);// 这句是为了防止弹出菜单获取焦点之后,点击activity的其他组件没有响应// popupWindow.setAnimationStyle(R.style.popup_show_out);popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);popupWindow.getContentView().findViewById(R.id.main).setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {// TODO Auto-generated method stubLog.i("lxm", "key press");switch (event.getAction()) {case KeyEvent.ACTION_DOWN:if (keyCode == KeyEvent.KEYCODE_MENU|| keyCode == KeyEvent.KEYCODE_BACK) {if (popupWindow != null) {popupWindow.dismiss();}}break;}return false;}});}
更多相关文章
- Android(安卓)调用WCF实例详解
- 【android】recycleview小整理
- Android中如何查看内存(下)
- 【转】android常用组件
- Android(安卓)笔记.代码段
- android组件
- android之组件4
- android 多媒体部分学习笔记十--简单视频播放
- android之【LinearLayout自定义View】