2011-06-09 33 views
4

在一個Honeycomb應用程序中,我在幾個地方使用PopupWindow的自定義子類來顯示各種視圖。這一切都很好,直到其中一個視圖碰巧嘗試顯示另一個PopupWindow。是否可以在另一個PopupWindow中顯示PopupWindow?

例如,Spinner和AutoCompleteTextView都使用PopupWindow來顯示其關聯的選項列表。如果你把這些在PopupWindow的觀點之一,並點擊激活窗口小部件時,窗口管理器,將通過logcat的警告你:

WARN/WindowManager(111): Attempted to add window with token that is a sub-window: [email protected] Aborting.

然後當它實際上試圖說明,PopupWindow它會拋出一個WindowManager$BadTokenException

自定義PopupWindow的視圖正在使用從錨視圖的上下文中獲取的LayoutInflater進行膨脹。我已經看到其他問題,表明在使用不合適的上下文獲取LayoutInflater時會出現BadTokenExceptions,但在這種情況下似乎沒有其他選項。

來自WindowManager的日誌警告似乎表明這是一個不受支持的情況。任何人都可以證實這一點,或提供一個棍子戳我正確的方向嗎?

下面是從錯誤的情況下發起的代碼(有些版本的它,反正)鏈接:WindowManagerService.java

+0

對此有何好運?我也試圖獲得類似的東西 – 2011-11-28 14:25:58

+0

沒有運氣。我相信這是根本不可能的,所以我轉而使用對話框或對話主題活動,我之前使用PopupWindow並需要子窗口功能。 – lyricsboy 2011-12-05 18:43:46

+0

我也是。我將我的Popup轉換爲Trasparent主題的活動,以實現我需要的功能。 – 2011-12-07 06:36:44

回答

-1

爲確保您可以從另一個內顯示PopupWindow PopupWindow,只需創建一個新的POP_UP窗口變量和膨脹PopupWindow佈局在新視圖,就像在下面的例子:

public PopupWindow pw; 
public PopupWindow new_pw; 
public View pw_layout = null; 
public View new_pw_layout = null; 

public void initiatePopupWindow() throws Exception{ 
// to get the instance of the LayoutInflater 
     LayoutInflater inflater = (LayoutInflater) YourCurrentActivityClass.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

pw_layout = inflater.inflate(R.layout.first_popup_xml, 
        (ViewGroup)findViewById(R.id.first_popup_main_layout)); 

// create a 300px width and 470px height PopupWindow 
pw = new PopupWindow(pw_layout, 
         ViewGroup.LayoutParams.WRAP_CONTENT, 
         ViewGroup.LayoutParams.WRAP_CONTENT, true); 
// display the popup in the center 
pw.showAtLocation(pw_layout, Gravity.CENTER, 0, 0); 

Button item_button = (Button) pw_layout.findViewById(R.id.item); 
       item_button .setOnClickListener(onItemlClick); 

} 


// the onClick listener for the item button 
public OnClickListener onItemlClick = new OnClickListener() { 
     public void onClick(View v) { 

if(v == item_button){ 
LayoutInflater inflater = (LayoutInflater) YourCurrentActivityClass.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
new_pw_layout = inflater.inflate(R.layout.second_popup_xml, 
        (ViewGroup)findViewById(R.id.second_popup_main_layout)); 
// create a 300px width and 470px height new PopupWindow 
new_pw = new PopupWindow(new_pw_layout, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, true); 
// display the new popup in the center 
new_pw.showAtLocation(new_pw_layout, Gravity.CENTER, 0, 0);// you can set it's position here([0,0] means in the exact center it's like [go 0 pixels from the center to the right , go 0 pixels from the center down]) 

} 
} 
}; 
+0

我不認爲這回答了確切的問題。您的示例代碼顯示了兩個PopupWindows,但它們都通過您的Activity進行了充氣。在你的情況下,第一個PopupWindow不包含第二個PopupWindow的視圖。我的問題中的示例場景涉及到一個框架View(如Spinner),它試圖在我創建並顯示的PopupWindow內顯示一個PopupWindow(用於項目列表)。 – lyricsboy 2014-02-26 14:49:13

+0

它們都從主要活動膨脹,但第二個彈出式視圖僅通過處理第一個彈出式視圖(如按下該彈出式視圖上的按鈕)顯示,而第二個彈出式視圖將顯示在第一個彈出式視圖上。 – MRefaat 2014-02-27 10:23:08