gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:51 27 4
gpt4 key购买 nike

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

例如,Spinner 和 AutoCompleteTextView 都使用 PopupWindow 来显示其关联的选择列表。如果您将其中一个放在 PopupWindow 的 View 中,并单击以激活该小部件,则 WindowManager 将通过 LogCat 警告您:

WARN/WindowManager(111):尝试添加带有子窗口 token 的窗口:android.os.BinderProxy@40ea6880。中止。

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

自定义 PopupWindow 的 View 正在使用从 anchor View 的 Context 获得的 LayoutInflater 进行扩充。我看到其他问题表明在使用不适当的 Context 获取 LayoutInflater 时可能会发生 BadTokenExceptions,但在这种情况下似乎没有其他选择。

来自 WindowManager 的日志警告似乎表明这是一个不受支持的案例。谁能证实这一点,或者提供一根棍子让我朝正确的方向戳?

这里有一个指向错误案例起源的代码(无论如何,它的某个版本)的链接:WindowManagerService.java

最佳答案

确定您可以从另一个 PopupWindow 中显示一个 PopupWindow,只需创建一个新的 pop_up 窗口变量并inflate 新的 View 中的 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])

}
}
};

关于android - 是否可以从另一个 PopupWindow 中显示一个 PopupWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6296538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com