2012-06-15 47 views
12

我使用PopupWindow與EditText重疊播放菜單。PopupWindow z ordering

它工作正常,除了我的PopupWindow被來自EditText IME系統(選擇標記,粘貼按鈕)的一些項目重疊。

我的問題是:我如何強制我的PopupWindow的z順序,以便它出現在這些裝飾之上?

這是發生了什麼的圖像。我需要在任何事物上繪製我的PopupWindow(菜單),從而以某種方式告訴WindowManager如何訂購窗口。 謝謝。

enter image description here

+0

好問題。不幸的是,我認爲答案將會是你無法真正做到。如果有辦法做到這一點,我想它可能只適用於股票機器人。文本選擇器和上下文彈出窗口是製造商通常在其硬件上引入定製版本的一些東西。即使有辦法做到這一點,我也會猜想它不適用於所有設備類型,因爲它們已經對EditText進行了自定義。 – FoamyGuy

+0

定製是無關緊要的,從技術上講這些東西必須在Android級別繪製,我懷疑他們是一種android.view.Window並使用android.view.WindowManager。 –

回答

7

發現前面回答自己。

那些裝飾是普通的PopupWindow-s,由EditText管理。

任何窗口的Z排序由WindowManager.LayoutParams.type定義,實際上它定義了Window的目的。彈出窗口的有效範圍爲FIRST_SUB_WINDOW - LAST_SUB_WINDOW。

除了使用Java反射調用隱藏函數PopupWindow.setWindowLayoutType(int)並設置所需的窗口類型外,應用程序通常不能更改PopupWindow的「類型」。

結果:

enter image description here

編輯: 代碼,不會說:

Method[] methods = PopupWindow.class.getMethods(); 
    for(Method m: methods){ 
    if(m.getName().equals("setWindowLayoutType")) { 
     try{ 
      m.invoke(getPopupWindow(), WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     break; 
    } 
    } 
+0

您能否提供一個代碼片段,您是如何做到的? – halxinate

2
public void compatibleSetWindowLayoutType(int layoutType) { 
    if (Build.VERSION.SDK_INT >= 23) { 
     setWindowLayoutType(layoutType); 
    } else { 
     try { 
      Class c = this.getClass(); 
      Method m = c.getMethod("setWindowLayoutType", Integer.TYPE); 
      if(m != null) { 
       m.invoke(this, layoutType); 
      } 
     } catch (Exception e) { 
     } 
    } 
} 
0
import android.support.v4.widget.PopupWindowCompat; 

PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);