• show drop down menu within/from action bar


    show drop down menu within/from action bar

    show drop down menu within/from action bar

    Solution

    When you want to anchor popupmenu to ActionItem in ActionBar you need to find view that renders ActionItem. Simple find view with findViewById() where id is same as id of your menu item in xml.

    DISPLAYING POPUP

    public boolean onOptionsItemSelected(MenuItem item) {
        // ...
    
        View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
        PopupMenu popupMenu = new PopupMenu(this, menuItemView); 
        popupMenu.inflate(R.menu.counters_overflow);
        // ...
        popupMenu.show();
        // ...
        return true;
    }
    

    MENU

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
         ....
    
         <item
        android:id="@+id/menu_overflow"
        android:icon="@drawable/ic_overflow"
        android:showAsAction="ifRoom"
        android:title="@string/menu_overflow"/>
    
         ....
    
    </menu>
    

    or you can dynamically genereate the popup menu as this:

    View menuItemView = findViewById(R.id.menu_dropdown); // SAME ID AS MENU ID
    
    PopupMenu popup = new PopupMenu(MainActivity.this, menuItemView);
    
    android.view.Menu menu = popup.getMenu();
    menu.add("test").setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                return true;
            }
        });
    }
    
    popup.show();
    

    There are more usage availble here.


    Post by: Jalen Wang (转载请注明出处)

  • 相关阅读:
    GO语言(golang)官方网站!
    Android官方网站!
    如何使用Gmail的别名功能?
    函数指针
    单例 ------ JAVA实现
    网络通信方案 ------ 以太网通信软硬件实现方案
    nginx的启动和关闭
    FineReport软件
    nginx的MainLine version、Stable version、Legacy versions
    linux常用命令
  • 原文地址:https://www.cnblogs.com/jalenwang/p/3400527.html
Copyright © 2020-2023  润新知