是否可以从另一个PopupWindow中显示PopupWindow?

时间:2011-06-09 17:00:30

标签: android android-3.0-honeycomb popupwindow

在Honeycomb应用程序中,我在几个地方使用PopupWindow的自定义子类来显示各种视图。这一切都很有效,直到其中一个视图碰巧尝试显示另一个PopupWindow。

例如,Spinner和AutoCompleteTextView都使用PopupWindow来显示其关联的选择列表。如果您将其中一个放入PopupWindow的视图中,然后单击以激活窗口小部件,WindowManager将通过LogCat警告您:

WARN/WindowManager(111): Attempted to add window with token that is a sub-window: android.os.BinderProxy@40ea6880. Aborting.

然后它会在实际尝试显示PopupWindow时抛出WindowManager$BadTokenException

使用从锚点视图的Context获得的LayoutInflater来扩展自定义PopupWindow的视图。我已经看到其他问题表明使用不适当的Context来获取LayoutInflater时可能会发生BadTokenExceptions,但在这种情况下似乎没有其他选项。

来自WindowManager的日志警告似乎表明这是一个不受支持的案例。任何人都可以证实这一点,或者提供一根棍子来指引我正确的方向吗?

以下是错误案例源自的代码(无论如何)的链接:WindowManagerService.java

1 个答案:

答案 0 :(得分:-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])

}
}
};