【问题标题】:How I can place overflow menu below toolbar instead of overflow menu to overlaps the app bar如何在工具栏下方放置溢出菜单而不是溢出菜单以与应用栏重叠
【发布时间】:2015-04-04 10:57:52
【问题描述】:

我知道这是根据材料指南,但我不喜欢它,我希望它位于工具栏下方。请提供一些信息来调整溢出菜单的位置。

【问题讨论】:

    标签: android android-layout android-toolbar


    【解决方案1】:

    在你的主要风格中使用<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>,其中

    <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>
    

    对于 Lollipop 样式必须在 values-v21 中。

    【讨论】:

    【解决方案2】:

    使用上述解决方案时,我遇到的问题是菜单项背景是透明的,而且在我单击菜单项之前,它会阻止窗口上的每个操作。对于那些遇到我的问题的人,我建议在菜单项中添加一个弹出菜单。例如,我的 menu.xml 上有这个项目:

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

    然后,在我的 OnOptionsItemsSelected 方法上(将我的菜单膨胀到活动之后):

    @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);
    }
    

    使用此解决方案,菜单选项始终显示在单击的菜单项下方。如果你有任何问题,尽管问我!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      相关资源
      最近更新 更多