2014-09-03 148 views

回答

0

好,與它搏鬥後,我找到了一種方法來做到這一點。這是有點棘手LOL。

public void createWindow() { 
    //initialize the popup window 
    // blah blah blah and right before you're about to show it, create 
    // a framelayout like the one below. And use windowmanager to addView 
    // (just like the way we create a floating icon on top of other apps) 
    framelayout = new FrameLayout(this); 
    WindowManager.LayoutParams layoutparameters = new WindowManager.LayoutParams(
      width, height, WindowManager.LayoutParams.TYPE_PHONE, 
      WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
      PixelFormat.TRANSLUCENT); 

    parameters.gravity = Gravity.TOP | Gravity.LEFT; 
    manager.addView(framelayout, layoutparameters); 
    framelayout.post(new Runnable() { 
     public void run() { 
      pop.showAtLocation(framelayout, Gravity.CENTER, 0, 0); 
     } 
    }); 
    } 

但不這樣做,我的朋友們!不要在方法CreateWindow的()。

pop.setOnDismissListener(new OnDismissListener() { 

     @Override 
     public void onDismiss() { 
      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        try { 
         manager.removeView(framelayout); 
         framelayout = null; 
        } catch (Exception e) { 

        } 
       } 
      }, 500); 

忘記了這一點你可以離開了處理程序。我使用它,因爲我的彈出窗口中使用動畫解僱(是我的動畫時長爲500毫)。完成!

1

搭上這麼多的功能後,得到了預期的結果。而不是使用ShowAtLocation()使用showAsDropDown()。定義錨點,寬度和高度。如果你的min sdk大於16,那麼你可以使用重力作爲第四個參數。

popupWindow.showAsDropDown(mcontext.findViewById(R.id.ap1), LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);