zoukankan      html  css  js  c++  java
  • 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 (转载请注明出处)

  • 相关阅读:
    LoadRunner11 问题记录
    安装依赖包时,编译提示依赖库未安装,但实际上已经安装
    Close_wait进程过多
    JS中的混合模式
    JS面相对象
    JS中的工厂模式
    函数式编程Map()&Reduce()
    JavaScript函数参数与调用
    JavaScript中的prototype
    循环
  • 原文地址:https://www.cnblogs.com/jalenwang/p/3400527.html
Copyright © 2011-2022 走看看