我有一个项目,我希望始终使用AppCompat库在操作栏中显示.它只是一个使用默认溢出图像的子菜单.
这在API级别v8-13上对我不起作用,我已经完成了开发人员指南所说的一切,包括添加我自己的命名空间.
我正在使用一个自定义样式,其中Theme.AppCompat作为其父级(如下).
这是我的第一篇帖子,所以我没有足够的声望点来发布活动图像,但是当我按下菜单按钮时,操作栏中缺少该项目,菜单显示在底部.
任何反馈将不胜感激!!
AndroidManifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myproject" android:installLocation="auto" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:debuggable="true" android:icon="@drawable/my_app_icon" android:label="@string/app_name" android:theme="@style/CustomActionBarTheme" > <uses-library android:name="com.google.android.maps" /> <activity android:name=".ui.MainActivity" android:label="@string/app_name" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
menu.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:MyApp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/overflow" MyApp:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark" MyApp:showAsAction="always" android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark" android:showAsAction="always"> <menu> <item android:id="@+id/action_about" android:icon="@drawable/about_icon" android:title="About"/> <item android:id="@+id/action_Feedback" android:icon="@drawable/Feedback_icon" android:title="Feedback"/> <item android:id="@+id/action_settings" android:icon="@drawable/settings_icon" android:title="@string/action_settings"/> </menu> </item> </menu>
styles.xml
<resources> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat"> <item name="android:actionBarStyle">@style/MyActionBar</item> <item name="actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar"> <item name="android:titleTextStyle">@style/TitleTextStyle</item> <item name="android:background">@drawable/abc_ab_bottom_solid_dark_holo</item> <item name="titleTextStyle">@style/TitleTextStyle</item> <item name="background">@drawable/abc_ab_bottom_solid_dark_holo</item> </style> <style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textStyle">bold</item> </style> </resources>
MainActivity.java
package com.myproject; ... public class MainActivity extends BaseActivity { public void onCreate(Bundle savedInstanceState) { super.onCreateEqually(savedInstanceState); ActivityHelper.setLayoutTitle(this,R.layout.main,R.string.main,getSupportActionBar()); } ... }
BaseActivity.java
package com.myproject; import android.support.v7.app.ActionBarActivity; ... public class BaseActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public boolean onCreateOptionsMenu(Menu menu) { return true; } public boolean onPrepareOptionsMenu(Menu menu) { return MenusHelper.displayMenu(this,menu); } ... }
MenusHelper.java
package com.myproject; ... public final class MenusHelper { ... public static boolean displayMenu(BaseActivity currentActivity,Menu menu) { // clear former menus menu.clear(); MenuInflater inflater = new MenuInflater(currentActivity); inflater.inflate(R.menu.menu,menu); return true; } ... }
ActivityHelper.java
package com.myproject; import android.support.v7.app.ActionBar; ... public final class ActivityHelper { ... public static void setLayoutTitle(Activity activity,int layoutId,String titleStr,ActionBar actionBar) { activity.setContentView(layoutId); actionBar.setTitle(titleStr); actionBar.setDisplayHomeAsUpEnabled(true); } }
解决方法
尝试将代码放入MenusHelper.displayMenu(this,menu);进入onCreateOptionsMenu()而不是onPrepareOptionsMenu()并进行必要的修改.
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu,menu); // In case you have an item MenuItem shareItem = menu.findItem(R.id.menu_share); // To retrieve the Action Provider mActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); return super.onCreateOptionsMenu(menu); }
确保XML中的菜单如下所示:
<item android:id="@+id/share" android:title="@string/menu_share" yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" yourapp:showAsAction="ifRoom|withText"/>