【问题标题】:How to make my menu to appear below the Action Bar area如何使我的菜单显示在操作栏区域下方
【发布时间】:2015-08-24 06:45:12
【问题描述】:

我的问题是如何将我的菜单下拉菜单移动到粉红色区域的空间(见第二张图片)。好像现在它也出现在我不想要的操作栏区域中。我希望我的菜单是出现在操作栏的正下方。

请大家帮我解决这个问题。

菜单就是这样来的。

我希望我的菜单显示在操作栏的正下方。

这就是我的 menu.xml 的样子

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.mobileinventorysuit.MainActivity" >

    <item
        android:id="@+id/login_mnu_imports"
        android:icon="@drawable/import_menu"
        app:showAsAction="always"
        style="@style/OverflowMenu"
        android:title="Import"/> 
   <item
        android:id="@+id/login_mnu_settings"
        android:icon="@drawable/settings_menu"
        style="@style/OverflowMenu"
        app:showAsAction="always"
        android:title="Settings"/>
     <item
        android:id="@+id/login_mnu_dev"
        android:icon="@drawable/dev"
        style="@style/OverflowMenu"
        app:showAsAction="always"
        android:title="Device Info"/>
    <item
        android:id="@+id/login_mnu_exit"
        style="@style/OverflowMenu"
        app:showAsAction="always"
        android:icon="@drawable/ic_lock_power_off"
        android:title="Exit"/>


</menu>

【问题讨论】:

  • 您可以从菜单创建新的 xml 布局。然后您可以将该布局包含在 currnet 主 xml 中。首先将其设置为隐藏。添加按钮或任何图像视图并在单击该视图时显示您的菜单。我已经完成了很多项目。
  • 能否请您给我一些链接或参考,因为我是小蜜蜂...这对我有帮助。 @Raj Sharma
  • 这个链接会很有帮助stackoverflow.com/questions/26979476/…

标签: android android-layout android-activity


【解决方案1】:

实际上ActionBar 菜单应该显示在操作栏的顶部。 来自Documentation

菜单是一张始终与应用栏重叠的临时纸, 而不是作为应用栏的扩展。

如果您仍然想要这种行为,您可以使用它。但不推荐。

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow" >
         <item name="overlapAnchor">false</item>
         <item name="dropDownVerticalOffset">?attr/actionBarSize</item>
</style>

查看这个答案Why is my overflow dropdown menu on top of the actionbar?

编辑

如果你想把图标放在你的ToolBar

将此添加到menu.xml

 xmlns:app="http://schemas.android.com/apk/res-auto"

并在您的菜单项中添加这一行

 app:showAsAction="always"

【讨论】:

  • 你提到的参考清除了我的全部疑虑......谢谢
  • 我也在尝试在我的菜单中显示图标但没有出现...你知道为什么吗?@ Boss
  • 我试过了,但是在 menuItem @ Boss 之前我看不到任何图标
  • 您想要一个带有菜单项的图标,还是只是操作栏中的一个图标??
  • 菜单项前@Boss
【解决方案2】:

转到 res => values => 样式并添加我在下面的评论中指出的那两行。它会帮助你

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

     //add these two line scripts and it will work properly

    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">?attr/actionBarSize</item>
</style>

【讨论】:

  • 欢迎来到 SO!通常不接受仅代码的答案。始终尝试解释您的解决方案以及为什么这会增加当前答案的内容。
【解决方案3】:

我们可以在PopUpWindow中使用它:

   mWindow.showAsDropDown(anchor, getScreenWidth((Activity) mContext) - getPixelFromDp(mContext, 465), getPixelFromDp(mContext, 0));

  public static int getScreenWidth(Activity context){
     Display display = context.getWindowManager().getDefaultDisplay();
     return display.getWidth();
  }

   public static int getPixelFromDp(Context context, int dpUnits) {
      float pixel = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpUnits, getDisplayMetrics(context));
      return (int) pixel;
 }

【讨论】:

    【解决方案4】:

    我认为这是正确的

      <item name="overlapAnchor">false</item>
        <item name="android:dropDownVerticalOffset">?attr/actionBarSize</item>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多