长按操作栏打开弹出窗口

时间:2017-11-13 05:16:56

标签: android

right now it is showing like this. when I click on actionbar any where then it shows only at left corner. I want it to open below i click我想在较长时间点击操作栏时显示弹出式菜单,但正好在我点击的位置。

toolbar.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        PopupMenu popupMenu = new PopupMenu(HomeActivity.this,toolbar);
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.device_setting:
                        Toast.makeText(HomeActivity.this, "dev settings", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.app_update:
                        break;
                    case R.id.wireless_setting:
                        break;
                    case R.id.hide_footer:
                        break;
                }
                return true;
            }
        });
        popupMenu.show();
        return true;
    }
});

popup_menu.xml位于

之下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/device_setting"
        android:title="Device Setting"></item>
    <item android:id="@+id/app_update"
        android:title="App Update"></item>
    <item android:id="@+id/wireless_setting"
        android:title="Wireless Setting"></item>
    <item android:id="@+id/hide_footer"
        android:title="Full Screen"></item>
</menu>
屏幕截图如下。 我想在ActionBar上单击(长按)的下方打开弹出窗口。请帮忙

Image Screenshot

1 个答案:

答案 0 :(得分:0)

根据您的要求,您只需要使用弹出窗口,因为它非常灵活,我更喜欢这个GitHub库 https://github.com/kakajika/RelativePopupWindow
这里,只需要改变窗口的位置,例如

popup.showOnAnchor(toolbar, VerticalPosition.BELOW, HorizontalPosition.LEFT);