I\'m using ActionBarCompat in my app, an I want to show one or two items in the action bar
I follwed the guide of google developers, but when I test it, the items are sh
<item
android:id="@+id/ok"
android:icon="@drawable/ic_ok"
android:orderInCategory="0"
android:showAsAction="ifRoom"
android:title="OK"/>
This is also true for the Android Toolbar in new Material Design concepts in the AppCompat-v21 and API levels 21 and above.
We have two ways to solute this problem.
OR 2. You need add libs "android-Support-v7". If you choice this method ,then your menu.xml need like this:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:v7="http://schemas.android.com/apk/res-auto" >
<item
android:id="..."
android:icon="..."
android:showAsAction="always"
android:title="..."
v7:showAsAction="always"/>
</menu>
The question has been updated to include the answer, but for anyone who is interested in the official documentation, see http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems and pay particular attention to the note about using a custom namespace for the showAsAction
attribute.
I had the same problem, and found two solutions:
In the menu xml (Login.xml), use your app name for the showAsAction tag:
instead of:
<item
android:id="@+id/action_register"
android:showAsAction="always"
android:icon="@drawable/some_icon"
android:title="@string/login_menu_register" />
use:
<item
android:id="@+id/action_register"
yourappname:showAsAction="always"
android:icon="@drawable/some_icon"
android:title="@string/login_menu_register" />
I suppose your application's name is shady.
the second solution for me, on the activity class, at the onCreateOptionsMenu()
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(
getMenuInflater().inflate(R.menu.main, menu);
MenuItem registerMenuItem = menu.findItem(R.id.action_register);
registerMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); // change this in backcompat
return true;
}
if you are using backCompatibility, change last line:
MenuItemCompat.setShowAsAction(registerMenuItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
I think you need to provide the icon for these actions so they can appear in the action bar.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:shudy="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_register"
android:showAsAction="always"
android:icon="@drawable/some_icon"
android:title="@string/login_menu_register" />
<item
android:id="@+id/action_register2"
android:showAsAction="always"
android:icon="@drawable/some_icon2"
android:title="miau" />
</menu>