简体   繁体   中英

Popup Menu shows at left bottom of the screen

This question has been asked before here . I followed the steps in the answer, but it didn't work.

I have a button in a fragment. I want to show a popup menu when that button is clicked. I tried many ways, but the popup menu keeps showing at the left bottom of the screen.

This is the popup menu method:

    public void showPopup(View v) {
    //added library level 25
    //may conflict with later updates
    PopupMenu popup = new PopupMenu(getContext(), v, Gravity.END);
    popup.setGravity(Gravity.END);
    MenuInflater menuInflater = popup.getMenuInflater();
    menuInflater.inflate(R.menu.menu_home_fragment, popup.getMenu());

    //handling items of the popup menu:
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();

            //no inspection Simplifiable If Statement
            if (id == R.id.menu_about_app) {
                //startActivity(new Intent(MainActivity.this, AboutDietCornerActivity.class));
            }
            if (id == R.id.menu_rate_app) {
                Intent i = new Intent(android.content.Intent.ACTION_VIEW);
                i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.techdroidltd.dietcorner"));
                startActivity(i);
            }
            if (id == R.id.menu_user_ideas) {
                //startActivity(new Intent(MainActivity.this, UserIdeasActivity.class));
            }
            if (id == R.id.menu_privacy_policy) {
                //startActivity(new Intent(MainActivity.this, PrivacyPolicyActivity.class));
            }
            return false;
        }
    });
    //end of handling items .. then show popup:
    popup.show();
}

Then I created a button inside the onCreateView of the fragment to call that method:

mainMenuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopup(getView());
        }
    });

The popup menu shows and works well. The problem that it shows in the wrong place, no matter how I change its gravity. Plz help.

Just referring to the documentation, the pop menu view is always anchored to views, it will popup / appear below or above the anchored view. As in the documentation,

A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.

PopupMenu

So, in your case I have tested it is anchored to the instance of view created by getView(); I believe your PopupMenu is anchored to instance view of mainMenuButton, or whatever created by getView();

Because you are creating the instance of popup menu with the following constructor PopupMenu(Context context, View anchor, int gravity) , so it means you are anchoring your popup to certain view, above or below which the popup menu will appear.

setGravity(int gravity)

Sets the gravity used to align the popup window to its anchor view.

It is just for you to understand what is actually happening. So, trivial solution would be to put the anchor view in the position where your popup view will appear, then modify the align relative to anchor, to modify the position relative to anchor view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM