2014-10-29 140 views
13

这是我的第一个堆栈溢出问题。如果我没有添加足够的数据或者我添加了太多细节,那就和我一起。位于操作栏/工具栏上方的Android溢出菜单?

我想让我的溢出菜单出现在我的应用程序顶部栏下方。当我使用Holo主题时,它做得很好,但我试图让我的应用程序使用appcompat v7库来使用材质设计。

所以,我的主题,现在从程序兼容性扩展:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <!-- the theme applied to the application or activity --> 
    <style name="CustomActionBarTheme" 
      parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="colorPrimary">@color/ucla_blue</item> 
     <item name="colorAccent">@color/caldroid_white</item> 
     <item name="android:actionMenuTextColor">@color/caldroid_black</item> 
     <item name="android:windowNoTitle">true</item> 
     <item name="windowActionBar">false</item> 
    </style> 
</resources> 

这正常不应用材质的设计主题,以我的工具栏在此布局:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/linearlayout_root_main" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 
    <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> 
    <android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/drawer_layout" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     tools:context="com.ucla.ieee.app.MainActivity"> 

     <LinearLayout 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:orientation="vertical"> 

      <android.support.v7.widget.Toolbar 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:background="?attr/colorPrimary" 
       android:id="@+id/toolbar" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:minHeight="?attr/actionBarSize" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 
      <!-- As the main content view, the view below consumes the entire 
      space available using match_parent in both dimensions. --> 
      <FrameLayout 
       android:id="@+id/container" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent"/> 
     </LinearLayout> 


     <!-- The navigation drawer --> 
     <fragment 
      android:id="@+id/navigation_drawer" 
      android:layout_gravity="start" 
      android:layout_height="match_parent" 
      android:layout_marginTop="?attr/actionBarSize" 
      android:layout_width="@dimen/navigation_drawer_width" 
      android:name="navigation.NavigationDrawerFragment" 
      tools:layout="@layout/fragment_navigation_drawer"/> 

    </android.support.v4.widget.DrawerLayout> 
</LinearLayout> 

但由于某些原因,菜单我在onCreateOptionsMenu中膨胀出现在错误的地方。显然我不能发布图像,但基本上溢出菜单覆盖工具栏,而不是出现在工具栏下方。当我在普通的ActionBar中首先使用“Theme.AppCompat”,甚至现在使用工具栏时,就开始发生这种情况。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      switch (selected) { 
       case FRONT_PAGE: 
        toolbar.inflateMenu(R.menu.main_settings); 
        return false; 
       case ANNOUNCEMENTS: 
        toolbar.inflateMenu(R.menu.refresh_settings); 
        return false; 
       case MEMBERSHIP: 
        toolbar.inflateMenu(R.menu.edit_member); 
        return false; 
       case CALENDAR: 
        toolbar.inflateMenu(R.menu.refresh_settings); 
        return false; 
       case POINTS_REWARDS: 
        toolbar.inflateMenu(R.menu.main_settings); 
        return false; 
       case HELP: 
        toolbar.inflateMenu(R.menu.main_settings); 
        return false; 
       default: 
        toolbar.inflateMenu(R.menu.main_settings); 
      } 

似乎找不到任何人遇到这种情况,我希望有人能帮我弄清楚这一点!谢谢!

这是我的菜单中的一个的示例:

<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.ucla_ieee.app.MainActivity"> 
    <item android:id="@+id/action_refresh" 
      android:title="@string/action_refresh" 
      app:showAsAction="never" /> 
</menu> 

回答

19

Material Design specifications(请参阅菜单部分):

菜单是一张临时片总是重叠应用栏,而不是作为应用栏的扩展。

Menu overlap app bar

所以你看到的是正确的材质设计菜单。

+1

谢谢,但我已经在我的主要活动中这样做了。这并不是菜单不起作用,它只是看起来不正确:http://i.imgur.com/0ybW1A9.png – Tania 2014-10-29 20:17:07

+0

根据您的屏幕截图更新了我的答案:您所看到的是一个Material Design菜单。 – ianhanniballake 2014-10-29 20:22:09

+0

哦,该死,猜猜我实施Material Design的应用程序给了我错误的想法。谢谢!! – Tania 2014-10-29 20:34:30

0

如果更改活动的主题清单比溢出菜单自带的动作条

android:theme="@android:style/Theme.Holo.Light" 
0

一些简单的步骤如下 转到Android清单文件,然后搜索中,你的下拉菜单中出现的活动,矿用它的欢迎活动 现在按下控制,并单击主题名称

<activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

您将移动到文件的样式在您的主题是写 一次ÿ OU去那里,你

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

现在粘贴这两个项目有

<item name="overlapAnchor">false</item> 
<item name="android:dropDownVerticalOffset">5.0dp</item> 

和你做......

P.S:不幸的是我不能在这里分享屏幕截图,看起来好像是在添加图像时出现了一些错误。但你可以看到这个链接 Image File

相关问题