ActionBar items appear always in Overflow Menu

前端 未结 7 779
一向
一向 2021-01-02 11:33

I am trying to add actions item in action bar, but items always are adding into the overflow list even there is lot of room. Any suggestion to resolve this ??

相关标签:
7条回答
  • 2021-01-02 11:45

    Try with android:showAsAction="always" for the items in the menu.

    0 讨论(0)
  • 2021-01-02 11:46

    My app is using the Support Library for compatibility on versions as low as Android 2.2 so I have to define my own XML namespace and use that namespace as the attribute prefix

    Solution was this, I was writing this android:showAsAction="ifRoom" but I should have app:showAsAction="ifRoom"

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item
            android:id="@+id/action_refresh"
            android:icon="@drawable/ic_action_refresh"
            app:showAsAction="ifRoom"
            android:title="@string/action_refresh" />
    
        <item
            android:id="@+id/action_search"
            android:icon="@drawable/ic_action_search"
            app:showAsAction="ifRoom"
            android:title="@string/action_search" />
    
        <item
            android:id="@+id/action_settings"
            android:title="@string/action_settings"
            app:showAsAction="never" />
    
    
    </menu>
    
    0 讨论(0)
  • 2021-01-02 11:47

    try this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="ifRoom"
        android:title="@string/action_refresh" />
    
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom"
        android:title="@string/action_search" />
    
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    

    0 讨论(0)
  • 2021-01-02 12:00

    I have the similar issue before you should try this

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <item
        android:id="@+id/action_refresh"
        app:showAsAction="never"
        android:title="@string/action_refresh" />
    
    <item
        android:id="@+id/action_search"
        app:showAsAction="never"
        android:title="@string/action_search" />
    
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    

    0 讨论(0)
  • 2021-01-02 12:01

    As per the document

    If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
        <!-- Search, should appear as action button -->
        <item android:id="@+id/action_search"
              android:icon="@drawable/ic_action_search"
              android:title="@string/action_search"
              yourapp:showAsAction="ifRoom"  />
        ...
    </menu>
    
    0 讨论(0)
  • 2021-01-02 12:02

    If you want to show all items always in actionbar then make your menu xml file like below:

    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="always"
        android:title="@string/action_refresh" />
    
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="always"
        android:title="@string/action_search" />
    
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="always" />
    

    0 讨论(0)
提交回复
热议问题