【问题标题】:Android: How to move a popup window and make this window not block other windowsAndroid:如何移动弹出窗口并使该窗口不阻塞其他窗口
【发布时间】:2012-05-08 05:00:15
【问题描述】:

我是安卓新手。这是我的问题。 有没有弹出窗口可以在android中移动?如何实现弹出窗口或其他可以下药的对话框。而且这个弹窗可以不挡住其他的窗口吗?当它弹出时,我仍然可以在主窗口中使用其他功能。

提前谢谢你

【问题讨论】:

    标签: android popup window block drag


    【解决方案1】:

    为此目的使用 PopupWindow 类请参考以下链接: http://www.mobilemancer.com/2011/01/08/popup-window-in-android/

    【讨论】:

    • 我已经检查了这个链接,但是那个弹出窗口不能是毒品。可以用手指移动吗?
    • 我们可以通过指定x,y位置来用代码改变popupwindow的位置
    • 谢谢,我刚刚发现我们可以定义onTouch来改变位置
    【解决方案2】:

    这是我工作中的示例代码,我做一些评论来说明它是如何工作的

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  // your custom view
                View popView = inflater.inflate(R.layout.pop_view, null);
                 // initialise your pop window  
                 // take custom view , with specific width , height
               PopupWindow popupWindow = new PopupWindow(popView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
                ImageView imageview = (ImageView) popView.findViewById(R.id.imageview);
                Button dismiss = (Button) popView.findViewById(R.id.disbutton);
                imageview.setImageURI(imagePath);
    
        // button used to open pop window 
                popupWindow.showAsDropDown(ChooseFile, 50, -30);
    
              // here is the part related to move popwindow    
                popView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent event) {
    
                        int orgX = 0, orgY = 0;
                        int offsetX, offsetY;
    
    
                        switch (event.getAction()) {
    
                            case MotionEvent.ACTION_DOWN:
    
                                orgX = (int) event.getRawX();
                                orgY = (int) event.getRawY();
                                break;
    
                            case MotionEvent.ACTION_MOVE:
                                offsetX = (int) event.getRawX() - orgX;
                                offsetY = (int) event.getRawY() - orgY;
                               // update popwindow postion , -1 set for width & height to make it fixed not changed 
                                popupWindow.update(offsetX, offsetY, -1, -1);
                                break;
    
                        }
    
                        return true;
                    }
                });
    
                dismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                      // if you want dismiss your pop window 
                        popupWindow.dismiss();
                    }
                });
    
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多