如何在 android 中获取弹出菜单的高度和宽度?

How to get height and width of a popup menu in android?

我有一个弹出菜单,我只想获取它的尺寸以在我的 showCaseView 中用于我的导览。我无法在任何地方找到确定这些尺寸(高度和宽度)的方法。

private void initPopUpMenu() {
    PopupMenu popupMenu = new PopupMenu(TimelineActivity.this, menuIcon);
    popupMenu.getMenuInflater().inflate(R.menu.menu_timeline, popupMenu.getMenu());
}

如何从此结构中检索宽度和高度?这是一个包含 5 个项目的简单菜单资源

最佳解决方案是使用 ListPopupWindow 而不是 PopupMenu,后者具有 getWidth()getHeight() 方法来获取其维度。但是,如果你真的要使用PopupMenu,可能的tricky方法是使用Reflection来访问其内部的ListView,因为PopupMenu中没有可用的方法来访问内容视图。

用法:

PopupMenu popupMenu = initPopUpMenu();
popupMenu.show();

ListView listView = getPopupMenuListView(popupMenu);

androidx.core.view.ViewKt.doOnLayout(listView, view -> {
    System.out.println("PopupMenu Size: " + view.getWidth() + " x " + view.getHeight());
    return null;
});

方法:

private PopupMenu initPopUpMenu() {
    PopupMenu popupMenu = new PopupMenu(TimelineActivity.this, menuIcon);
    popupMenu.getMenuInflater().inflate(R.menu.menu_timeline, popupMenu.getMenu());
    return popupMenu;
}

private ListView getPopupMenuListView(PopupMenu popupMenu) {
    Method getMenuListViewMethod = null;
    try {
        getMenuListViewMethod = PopupMenu.class.getDeclaredMethod("getMenuListView");
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

    ListView listView = null;
    if (getMenuListViewMethod != null) {
        getMenuListViewMethod.setAccessible(true);
        try {
            listView = (ListView) getMenuListViewMethod.invoke(popupMenu);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    return listView;
}

build.gradle:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.3.0'
}

结果:

I/System.out: PopupMenu Size: 539 x 660

您可以将自定义布局与 PopupWindow 一起使用,然后对其进行测量

val popupWindow = PopupWindow(context)
popupWindow.contentView = View.inflate(context, R.layout.my_popup, null).apply {
    measure(
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
    )
    measuredWidth// <-- here is width
    measuredHeight// <-- here is height
}