How to show a customized Popup window near the touch location, like what we have when we use ContextMenu?

前端 未结 2 1987
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 17:21

Background

Seeing that it\'s not officially possible to have a context menu which has a customized view or even icons for its rows (here), I decided to create my o

2条回答
  •  醉酒成梦
    2020-12-21 17:55

    It has been a while since I have done this, but I think we had the same problem. Let me see if I can answer.

    Not being able to make custom context menus for the EditText was one of the main reasons that I finally decided to create a library with custom components for Mongolian. Although the vertical Mongolian parts won't be useful to you, the concepts should be the same for other custom popups.

    Here are a couple screenshots of what I have:

    This one is a custom EditText that used a custom popup menu. It takes the user touch location to place the popup location.

    The next one is a more general demonstration of different ways to set the popup location.

    Both of these demos are included in the mongol-library demo app.

    My custom menu was a PopupWindow subclass. You can find the source code here.

    The way I placed it at a particular location was to use the showAtLocation method, which as I recall is just a normal method on PopupWindow:

    private void showMongolContextMenu(MongolMenu menu, int xTouchLocation, int yTouchLocation) {
        float paddingPx = CONTEXT_MENU_TOUCH_PADDING_DP * getResources().getDisplayMetrics().density;
        Rect menuSize = menu.getDesiredSize();
        int y = yTouchLocation - menuSize.height() - (int) paddingPx;
        menu.showAtLocation(this, Gravity.NO_GRAVITY, xTouchLocation, y);
    }
    

    That code is from here.

    Oh, yes, and I also used this in custom keyboards:

    See these classes for more:

    • PopupKeyCandidate
    • PopupKeyCandidatesView
    • Keyboard

提交回复
热议问题