Action Items not showing in ActionBar with showAsAction=“ifRoom”

前端 未结 2 724
心在旅途
心在旅途 2020-12-31 09:27

I\'m trying to add some Action Items to my Support Action Bar. In my activity, I have also tabs added to the Action Bar.

This is an excerpt of the activity:

相关标签:
2条回答
  • 2020-12-31 10:09

    I found the problem. There was an error in the menu .xml file. In fact, I've added a new namespace:

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

    But then, I still refer the property as if it belongs to the android namespace:

    android:showAsAction="ifRoom"

    The right way to refer this property is to use:

    app:showAsAction="ifRoom"

    Cause it belongs to the namespace app.

    Here is the relevant part in documentation:

    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.)

    0 讨论(0)
  • 2020-12-31 10:28

    I found the same error, and i resolve it like this

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:showAsAction="ifRoom"
        android:icon="@android:drawable/ic_delete"
        android:title="Delete"/>
    

    add,xmlns:app="http://schemas.android.com/apk/res-auto" in the middle of the code, works for me :)

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