zoukankan      html  css  js  c++  java
  • popuwindow

    Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

    • AlertDialog的位置固定,而PopupWindow的位置可以随意
    • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

    PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

    • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
    • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
    • showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移




    复制代码
    /** 
         * 初始化popWindow
         * */
        private void initPopWindow() {
            View popView = inflater.inflate(R.layout.listview_pop, null);
            popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0));
            //设置popwindow出现和消失动画
            popupWindow.setAnimationStyle(R.style.PopMenuAnimation);
            btn_pop_close = (ImageView) popView.findViewById(R.id.btn_pop_close);
            ll_pop_speech=(LinearLayout) popView.findViewById(R.id.ll_pop_speech);
            ll_pop_favor=(LinearLayout) popView.findViewById(R.id.ll_pop_favor);
            ll_pop_dislike=(LinearLayout) popView.findViewById(R.id.ll_pop_dislike);
            
            btn_pop_close.setOnClickListener(new popItemOnClickListener());
            ll_pop_speech.setOnClickListener(new popItemOnClickListener());
            ll_pop_favor.setOnClickListener(new popItemOnClickListener());
            ll_pop_dislike.setOnClickListener(new popItemOnClickListener());
        }
    复制代码
    复制代码
    /** 
         * 显示popWindow
         * */
        public void showPop(View parent, int x, int y,int postion) {
            //设置popwindow显示位置
            popupWindow.showAtLocation(parent, 0, x, y);
            //获取popwindow焦点
            popupWindow.setFocusable(true);
            //设置popwindow如果点击外面区域,便关闭。
            popupWindow.setOutsideTouchable(true);
            popupWindow.update();
            if (popupWindow.isShowing()) {
                
            }
            
        }
    复制代码
    复制代码
    /** 
         * 每个ITEM中more按钮对应的点击动作
         *
         **/
        public class popAction implements OnClickListener{
            int position;
            public popAction(int position){
                this.position = position;
            }
            @Override
            public void onClick(View v) {
                int[] arrayOfInt = new int[2];
                //获取点击按钮的坐标
                v.getLocationOnScreen(arrayOfInt);
                int x = arrayOfInt[0];
                int y = arrayOfInt[1];
                showPop(v, x , y, position);
            }
        }
  • 相关阅读:
    Bootstrap 2.2.2 的新特性
    Apache POI 3.9 发布,性能显著提升
    SQL Relay 0.48 发布,数据库中继器
    ProjectForge 4.2.0 发布,项目管理系统
    红帽企业 Linux 发布 6.4 Beta 版本
    红薯 快速的 MySQL 本地和远程密码破解
    MariaDB 宣布成立基金会
    Percona XtraBackup 2.0.4 发布
    Rocks 6.1 发布,光盘机群解决方案
    精通Servlet研究,HttpServlet的实现追究
  • 原文地址:https://www.cnblogs.com/xiaoxiaoshen/p/5236234.html
Copyright © 2011-2022 走看看