Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發步步為營之64:PopupWindow實現自定義彈出菜單

android開發步步為營之64:PopupWindow實現自定義彈出菜單

編輯:關於Android編程

打開PopupWindow的源碼,你會發現它其實也是通過WindowManager來添加view的。

 

       private void invokePopup(WindowManager.LayoutParams p) {
        if (mContext != null) {
            p.packageName = mContext.getPackageName();
        }
        mPopupView.setFitsSystemWindows(mLayoutInsetDecor);
        setLayoutDirectionFromAnchor();
        mWindowManager.addView(mPopupView, p);
       }

本文實現的效果就是通過PopupWindow彈出一個自定義menu,如圖所示:

 

\

點擊右上角的菜單,彈出菜單項,好,我們來實現這個效果。

第一步、設計菜單頁面layout_menu.xml

 


 


第二步、彈出菜單

 

 

 View popView = getLayoutInflater().inflate(R.layout.layout_menu, null);
        LinearLayout layoutShare = (LinearLayout) popView.findViewById(R.id.layout_share);
        layoutShare.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        LinearLayout layoutSetting = (LinearLayout) popView.findViewById(R.id.layout_setting);
        layoutSetting.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                startActivity(new Intent(MainActivity.this, SettingActivity.class));
                if (popup!=null&&popup.isShowing()) {
                    popup.dismiss();
                }
            }
        });

        LinearLayout layoutUpdate = (LinearLayout) popView.findViewById(R.id.layout_update);
        layoutUpdate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        PobupWindow popup = new PopupWindow(popView, UIUtils.dip2px(MainActivity.this, 156), UIUtils.dip2px(MainActivity.this, 150));
        popup.setFocusable(true);
        popup.setOutsideTouchable(true);
        popup.setBackgroundDrawable(new BitmapDrawable());
        layoutMenuContainer = (LinearLayout) findViewById(R.id.layout_menu);
        layoutMenuContainer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {

                  

                    int[] location = new int[2];
                    v.getLocationOnScreen(location);
                    if (popup.isShowing()) {
                        popup.dismiss();
                    } else {
                        // popup.showAsDropDown(v);
                        popup.showAsDropDown(v, -UIUtils.dip2px(MainActivity.this, 129), 0);
                        // popup.showAtLocation(v,
                        // Gravity.NO_GRAVITY,location[0]-UIUtils.dip2px(MainActivity.this,100),location[1]+UIUtils.dip2px(MainActivity.this,60));

                    }

                } catch (Exception e) {
                    if (e != null) {
                        e.printStackTrace();
                    }
                }

            }
        });


 

 


 

 

第三步、AndroidManifest.xml配置權限

 




 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved