操作栏 - 子菜单项,文本和图像无法正常工作

时间:2023-01-16 14:14:28

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.

我正在开发小型Android应用程序,我在其中使用带有一些菜单项的操作栏。一个菜单项包含子菜单。现在我想做的是始终显示带有文本和图标的菜单项。

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?

怎么做?有什么方法可以解决这个问题吗?

2 个解决方案

#1


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:

如果您使用的是Android Sopport Library(android.support.v7.app.ActionBar)中的ActionBar,则可能需要更改菜单和项目声明:

<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.

在为旧的Android版本提供兼容性时,我遇到了类似的问题。

#2


0  

better using android:showAsAction="ifRoom|withText" than always.
In that case depends on your text size and screen width.

更好地使用android:showAsAction =“ifRoom | withText”而不是总是如此。在这种情况下取​​决于您的文本大小和屏幕宽度。

#1


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:

如果您使用的是Android Sopport Library(android.support.v7.app.ActionBar)中的ActionBar,则可能需要更改菜单和项目声明:

<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.

在为旧的Android版本提供兼容性时,我遇到了类似的问题。

#2


0  

better using android:showAsAction="ifRoom|withText" than always.
In that case depends on your text size and screen width.

更好地使用android:showAsAction =“ifRoom | withText”而不是总是如此。在这种情况下取​​决于您的文本大小和屏幕宽度。