How I can place overflow menu below toolbar instead of overflow menu to overlaps the app bar

陌路散爱 提交于 2019-12-18 02:57:26

问题


I know that it is per material guideline, but i don't like it and i want it to below toolbar. please provide some info to adjust position of overflow menu.


回答1:


In your main style use <item name="actionOverflowMenuStyle">@style/OverflowMenu</item>, where

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
  <!-- Required for pre-Lollipop. -->
   <item name="overlapAnchor">false</item>
   <item name="android:dropDownVerticalOffset">-4.0dip</item>
  <!-- Required for Lollipop. -->
   <item name="android:overlapAnchor">false</item>
   <item name="android:dropDownVerticalOffset">4.0dip</item>
</style>

For Lollipop style must be in values-v21.




回答2:


With the above solution I had the problem that the menu item background was transparent and also It was kind of blocking every action on the window until I click the menu item. For the ones having my problem I suggest adding a popup menu to the menu item. For example, I have this item on my menu.xml:

<item
    android:id="@+id/new_work_report"
    android:icon="@drawable/ic_add_white"
    android:title="@string/action_settings"
    app:showAsAction="ifRoom"/>

Then, on my OnOptionsItemsSelected method (after inflating my menu to the activity):

@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
    switch (menuItem.getItemId())
    {
        case android.R.id.home:
            finish();
            break;

        case R.id.new_work_report:
            View itemView = FieldSheetActivity.this.findViewById(R.id.new_work_report);
            PopupMenu popMenu = new PopupMenu(MyActivity.this, itemView);
            popMenu.getMenu().add("Do something");

            popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
            {

                @Override
                public boolean onMenuItemClick(MenuItem item)
                {
                    // Your desired action

                    return true;
                }
            });
            popMenu.show();
            break;
    }

    return super.onOptionsItemSelected(menuItem);
}

With this solution, the menu options always are shown below the clicked menu item. In case you have any questions, just ask me!



来源:https://stackoverflow.com/questions/29445584/how-i-can-place-overflow-menu-below-toolbar-instead-of-overflow-menu-to-overlaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!