简体   繁体   中英

Android MenuItem showAsAction=“always” Ignored When Have a Group

I need implement a sort action on my application, maintaining the selected item checked. But when I use a group for it my main MenuItem begins to ignore the attribute showAsAction="always" .

Follows the layout used. I'm using the API 19 and AppCompat in version 21.0.2:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item 
        android:icon="@drawable/ic_content_sort"
        android:title="@string/menu_sort"
        app:showAsAction="always">
        <group android:checkableBehavior="single" >
            <item
                android:id="@+id/action_sort_by_date"
                android:title="@string/menu_sort_by_date" />
            <item
                android:id="@+id/action_sort_by_description"
                android:title="@string/menu_sort_by_description" />
        </group>
    </item>

</menu>

I found a solution on this related, but not equal, question: Having two single-selection groups in ActionBar doesn't work, but attaching a pop up menu instead doesn't work either .

With that, I had only to include the Group tag within a new tag Menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item 
        android:icon="@drawable/ic_content_sort"
        android:title="@string/menu_sort"
        app:showAsAction="always">
        <menu> <!-- SOLUTION -->
            <group android:checkableBehavior="single" >
                <item
                    android:id="@+id/action_sort_by_date"
                    android:title="@string/menu_sort_by_date" />
                <item
                    android:id="@+id/action_sort_by_description"
                    android:title="@string/menu_sort_by_description" />
            </group>    
        </menu>
    </item>

</menu>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM