Icon's priority on action bar (keeping order o

2019-07-20 20:36发布

问题:

I'm trying to control which action bar items get shown if there is limited space, and have tried the solution in this question and answer: Icons priority on action bar

Everything works fine other than I want to keep the order of the icons in a manner which is different to the priority, whereas the solution above causes the items to be reordered. Is there any way to set a priority for item display, without altering the order that items appear in the actionbar?

I've tried altering the order of the items in the xml, and this has no impact on the order of items as rendered (without the orderInCategory, changing the order in the file does result in a change in the order of items as rendered).

I'm developing against ICS.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_edit"
        android:icon="@android:drawable/ic_menu_edit"
        android:title="@string/action_edit"
        android:showAsAction="ifRoom"
        android:orderInCategory="1"
        />

    <item
        android:id="@+id/action_undo"
        android:icon="@android:drawable/ic_menu_revert"
        android:title="@string/action_undo"
        android:showAsAction="ifRoom"
        android:orderInCategory="2"
        />

    <item
        android:id="@+id/action_delete"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="@string/action_delete"
        android:showAsAction="ifRoom"
        android:orderInCategory="3"
        />

    <!-- I want this item to be on the right, 
    but shown in preference to any other items; 
    setting it's orderInCategory to 0 ensures that it 
    is shown in preference to other items, 
    but always places it on the left -->
    <item
        android:id="@+id/action_addnew"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/action_addnew"
        android:showAsAction="ifRoom"
        android:orderInCategory="0"
        />
</menu>

Thanks.

回答1:

The items are placed from Right to Left order so by rearranging the items, it can help

<item
    android:id="@+id/action_addnew"
    android:icon="@android:drawable/ic_menu_add"
    android:title="@string/action_addnew"
    android:showAsAction="always"
    android:orderInCategory="0"
    />
<menu>
    <item
        android:id="@+id/action_edit"
        android:icon="@android:drawable/ic_menu_edit"
        android:title="@string/action_edit"
        android:showAsAction="ifRoom"
        android:orderInCategory="1"
    />

    <item
        android:id="@+id/action_undo"
        android:icon="@android:drawable/ic_menu_revert"
        android:title="@string/action_undo"
        android:showAsAction="ifRoom"
        android:orderInCategory="2"
    />

    <item
        android:id="@+id/action_delete"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="@string/action_delete"
        android:showAsAction="ifRoom"
        android:orderInCategory="3"
    />
</menu>


回答2:

android:orderInCategory

This filed works from Right to Left so you need to right your code according to keep that in mind.



回答3:

What i got from reading your question is you want to display action_addnew should appear in the right & it should always appear to everyone.
Try this one

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/action_edit"
            android:icon="@android:drawable/ic_menu_edit"
            android:title="@string/action_edit"
            android:showAsAction="ifRoom"
            />

        <item
            android:id="@+id/action_undo"
            android:icon="@android:drawable/ic_menu_revert"
            android:title="@string/action_undo"
            android:showAsAction="ifRoom" />

        <item
            android:id="@+id/action_delete"
            android:icon="@android:drawable/ic_menu_delete"
            android:title="@string/action_delete"
            android:showAsAction="ifRoom"
            />

        <item
            android:id="@+id/action_addnew"
            android:icon="@android:drawable/ic_menu_add"
            android:title="@string/action_addnew"
            android:showAsAction="always"
            />
    </menu>


回答4:

From Left to Right give first item

android:orderInCategory="0"

and second item

android:orderInCategory="1"

so on.