AppCompat_v7 Toolbar as actionbar not showing 'always' actions from menu, but API Toolbar does

后端 未结 3 1981
囚心锁ツ
囚心锁ツ 2021-02-01 03:05

After 2 days of struggling with the new API 21 Toolbar and appCompat_v7, I think I found a bug on it. If you have 2 actions on your menu like this:



        
相关标签:
3条回答
  • 2021-02-01 03:14

    put

    xmlns:app="http://schemas.android.com/apk/res-auto" int your menu tag

    like this

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

    then use app:showAsAction = "always"

    Remember that use app:showAsAction in all menu item not android:showAsAction

    `

    0 讨论(0)
  • 2021-02-01 03:21

    Per the Action Bar training, you have to use the app:showAsAction attributes rather than the android:showAsAction attribute:

    Notice that the showAsAction attribute above uses a custom namespace defined in the <menu> tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

    So your menu file should look like:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >
        <item
            android:id="@+id/action_test"
            app:showAsAction="always"
            android:icon="@drawable/ic_launcher"
            android:title="@string/action_settings"/>
    
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            app:showAsAction="never"
            android:title="@string/action_settings"/>
    </menu>
    
    0 讨论(0)
  • 2021-02-01 03:24

    first of all use the app:showAsAction instead of android:showAsAction (like @ianhanniballake) after that in onCreateActionMode after Inflating your menu setShowAsAction in code like this

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.your_menu_name, menu);
    menu.findItem(R.id.your_first_menu_id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menu.findItem(R.id.your_second_menu_id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
    } 
    

    i had the same problem so after working on it these lines of code works fine for me.

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