简体   繁体   English

弹出菜单显示在屏幕的左下角

[英]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:然后我在片段的 onCreateView 中创建了一个按钮来调用该方法:

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. PopupMenu 在锚定到视图的模式弹出窗口中显示菜单。 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.如果 IME 可见,则弹出窗口将不会与它重叠,直到它被触摸。 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();因此,在您的情况下,我已经测试过它锚定到由 getView() 创建的视图实例; I believe your PopupMenu is anchored to instance view of mainMenuButton, or whatever created by getView();我相信您的 PopupMenu 锚定到 mainMenuButton 的实例视图,或由 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.因为您正在使用以下构造函数PopupMenu(Context context, View anchor, int gravity)创建弹出菜单的实例,所以这意味着您将弹出菜单锚定到某个视图,弹出菜单将出现在该视图的上方或下方。

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.因此,简单的解决方案是将锚视图放在弹出视图将出现的位置,然后修改相对于锚的对齐,以修改相对于锚视图的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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