弹出窗口的Z轴顺序

14

我使用PopupWindow与菜单交互,它们会重叠在EditText上。

这个方法很好用,但是我的PopupWindow被EditText输入法系统中的一些项目(选择标记、粘贴按钮)遮挡了。

我的问题是:如何强制PopupWindow的z-ordering,使其出现在那些装饰物之上?

这里是发生的情况的图片。我需要我的PopupWindow(菜单)被绘制在最顶层,因此需要某种方式告诉WindowManager如何排列窗口。 谢谢。

enter image description here


好问题。不幸的是,我认为答案可能是你真的做不到。如果有一种方法可以做到,我想它可能只适用于原生Android系统。文本选择器和上下文弹出窗口是制造商通常在其硬件上引入定制版本的一些功能。即使有一种方法可以做到,我猜想由于他们对EditText所做的定制化,它可能无法适用于所有设备类型。 - FoamyGuy
定制化并不重要,从技术上讲,这些东西必须在Android层面上以某种方式绘制出来,我怀疑它们是android.view.Window和使用android.view.WindowManager。 - Pointer Null
4个回答

9

我已经找到了答案。

这些装饰品是普通的PopupWindow,由EditText管理。

任何窗口的Z顺序都由WindowManager.LayoutParams.type定义,实际上它定义了窗口的目的。 对于弹出窗口,有效范围为FIRST_SUB_WINDOW - LAST_SUB_WINDOW。

应用程序通常无法更改PopupWindow的“type”,除非使用Java反射调用隐藏函数PopupWindow.setWindowLayoutType(int),并设置所需的窗口类型。

结果:

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;
     }
  }

请您提供一段代码片段,展示您是如何实现的? - halxinate

4
import android.support.v4.widget.PopupWindowCompat;

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

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

让它们相辅相成。 在显示弹窗之前必须调用PopupWindowCompat.setWindowLayoutType API。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接