问题
I am developing small Android application in which I am using action bar with some menu items. One menu item contains sub menu. Now what I want to do is to display menu item with text and icon always.
I define menu items in following manner.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/card_menu"
android:icon="@drawable/abc"
android:showAsAction="withText|always"
android:title="cards">
<menu>
<item
android:id="@+id/C1"
android:title="C1"/>
<item
android:id="@+id/C2"
android:title="c2"/>
<item
android:id="@+id/C3"
android:title="C3"/>
</menu>
</item>
<item
android:id="@+id/notification"
android:actionLayout="@layout/notification_icon"
android:icon="@drawable/notification"
android:showAsAction="always"
android:title="Notifications"/>
<item
android:id="@+id/filter"
android:icon="@drawable/filter"
android:showAsAction="always"
android:title="Filter"/>
</menu>
Now what happens is my action-bar displays my items properly; only thing is that when window is in portrait mode it shows only image, and when window is in landscape mode it shows image and text both. But I want image and text in both mode.
How to do that? Is there any way to solve this?
回答1:
If you are using the ActionBar from the Android Sopport Library (android.support.v7.app.ActionBar), you might need to change your menu and item declaration from:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/notification"
android:actionLayout="@layout/notification_icon"
android:icon="@drawable/notification"
android:showAsAction="always"
android:title="Notifications"/>
to
<!-- Your menu -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourappname="http://schemas.android.com/apk/res-auto" >
<!-- Your item to be shown as action-->
<item
android:id="@+id/notification"
android:actionLayout="@layout/notification_icon"
android:icon="@drawable/notification"
android:showAsAction="always"
yourappname:showAsAction="always"
android:title="Notifications"/>
I had a similar problem when providing the compatibility to older android versions.
回答2:
better using android:showAsAction="ifRoom|withText"
than always
.
In that case depends on your text size and screen width.
来源:https://stackoverflow.com/questions/14932216/action-bar-sub-menu-item-with-text-and-image-not-working-properly