我使用PopupWindow与菜单交互,它们会重叠在EditText上。
这个方法很好用,但是我的PopupWindow被EditText输入法系统中的一些项目(选择标记、粘贴按钮)遮挡了。
我的问题是:如何强制PopupWindow的z-ordering,使其出现在那些装饰物之上?
这里是发生的情况的图片。我需要我的PopupWindow(菜单)被绘制在最顶层,因此需要某种方式告诉WindowManager如何排列窗口。 谢谢。
我使用PopupWindow与菜单交互,它们会重叠在EditText上。
这个方法很好用,但是我的PopupWindow被EditText输入法系统中的一些项目(选择标记、粘贴按钮)遮挡了。
我的问题是:如何强制PopupWindow的z-ordering,使其出现在那些装饰物之上?
这里是发生的情况的图片。我需要我的PopupWindow(菜单)被绘制在最顶层,因此需要某种方式告诉WindowManager如何排列窗口。 谢谢。
我已经找到了答案。
这些装饰品是普通的PopupWindow,由EditText管理。
任何窗口的Z顺序都由WindowManager.LayoutParams.type定义,实际上它定义了窗口的目的。 对于弹出窗口,有效范围为FIRST_SUB_WINDOW - LAST_SUB_WINDOW。
应用程序通常无法更改PopupWindow的“type”,除非使用Java反射调用隐藏函数PopupWindow.setWindowLayoutType(int),并设置所需的窗口类型。
结果:
编辑: 执行此操作的代码:
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;
}
}
import android.support.v4.widget.PopupWindowCompat;
PopupWindowCompat.setWindowLayoutType(popupWindow, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
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) {
}
}
}
让它们相辅相成。
在显示弹窗之前必须调用PopupWindowCompat.setWindowLayoutType
API。