Action Bar not showing action view icons

后端 未结 2 653
走了就别回头了
走了就别回头了 2020-12-03 07:10

I\'m using the new Action Bar Support and all my action views are shown in overflow and not as icons in the bar. My app is for 7+ API.

HomeActivity:

         


        
相关标签:
2条回答
  • 2020-12-03 07:17

    you have to define your menu resource files with also the attributes for the support library.

    To implement the back support it reads them instead of the ones defined in older Android version.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        yourapp:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/action_browse"
        android:orderInCategory="100"
        android:showAsAction="never"
        yourapp:showAsAction="never"
        android:title="@string/title_activity_browse"/>
    <item
        android:id="@+id/action_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="ifRoom|collapseActionView"
        yourapp:showAsAction="ifRoom|collapseActionView"
        yourapp:actionViewClass="android.support.v7.widget.SearchView"
        android:title="@string/text_search"/>
    <item
        android:id="@+id/action_scan"
        android:icon="@drawable/action_scan"
        android:showAsAction="always"
        yourapp:showAsAction="always"
        android:title="@string/title_activity_browse"/>
    
    </menu>
    

    NB remeber that for the SearchView class changed. it's now used the one from the support library so you also have to update your code in the onCreateOptionsMenu()

    EDIT: here is a pretty good tutorial on how to migrate from ActionBarSherlok to AppCompat

    0 讨论(0)
  • 2020-12-03 07:17

    A common mistake is forgetting to include the title string in your string.xml file. Make sure you include it for each menu item.

    Suppose our menu xml contains the following item :

        <item
        android:id="@+id/toggle_action"
        android:orderInCategory="1"
        android:showAsAction="ifRoom"
        android:title="@string/toggle_action"/>
    

    If we forget to include the value for the string toggle_action, then the action bar icon will not show.

    Go to your strings.xml file and add in the following:

        <string name="toggle_action">TOGGLE</string>
    
    0 讨论(0)
提交回复
热议问题