问题
I have one item I want to always display in the action bar using the AppCompat library. It's just a sub menu using the default overflow image.
This is not working for me on API levels v8-13 and I've done everything the developer guide says, including adding my own namespace.
I'm using a custom style which has Theme.AppCompat as its parent (below).
This is my first post so I don't have enough reputation points to post an image of the activity, but the item is missing from the action bar and the menu appears on the bottom when I press the menu button.
Any feedback would be greatly appreciated!!
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);
}
}
回答1:
Try to put the code for MenusHelper.displayMenu(this, menu);
into the onCreateOptionsMenu()
rather than onPrepareOptionsMenu()
and also do the necessary modification.
@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);
}
Make sure your Menu in your XML looks like this:
<item
android:id="@+id/share"
android:title="@string/menu_share"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
yourapp:showAsAction="ifRoom|withText"/>
回答2:
the problem is this
android:showAsAction="always"
that needs to be
youpackagename:showAsAction="always"
actually looking over it again you declared that twice
android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark"
android:showAsAction="always"
remove that bit
来源:https://stackoverflow.com/questions/18254629/appcompat-item-not-showing-in-action-bar-for-api-levels-v8-13