简体   繁体   中英

Should use android:showAsAction when not using the appcompat library

I have a weird problem, I was using a menu.xml file in my android application like this:

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

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:title="@string/action_refresh"
        android:icon="@drawable/toc"
        compat:showAsAction="always"/>
</menu>

and it was working well, but some days ago i was updating my eclipse to the last version, and after that, the app:showAsAction="always" shows the error:

Should use android:showAsAction when not using the appcompat library and after that my action bar icons was moved to the overflow bar and not showing the in the action bar at all.

anyway, the appcompat library is the parent of my base style. what should I do to resolve that?

You need to do three things-

  1. Add these 3 things xmlns:tools="schemas.android.com/tools" tools:context="com.example.sample.YourActivity" xmlns:yourapp="http://schemas.android.com/apk/res-auto" to your <menu> tag

    along with the usual xmlns:android="http://schemas.android.com/apk/res/android"

  2. Add yourapp:showAsAction="ifRoom" to your <item> .

  3. Finally, clean your project via Project -> Clean...

The last step is what solved it for me. Simply cleaning the project (they can get so dirty sometimes)

This is what the final code looks like:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
tools:context="com.example.sample.YourActivity"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/action_share"
    android:orderInCategory="100"
    android:title="Share"
    android:icon="@drawable/ic_share_white"
    yourapp:showAsAction="ifRoom" />

</menu>

This works for me i have used appcompatlibrary:

compat:showAsAction="always"

instead of:

app:showAsAction="always"

And to use compat:showAsAction="always" , inside menu.xml include the line:

<menu   
   ......
  <!-- include the below line -->
    xmlns:compat="http://schemas.android.com/apk/res-auto" >

Faced the same problem. These two options worked for me:

1.Including - xmlns:compat="http://schemas.android.com/apk/res-auto" > and then using:"compat:showAsAction="ifRoom"/>"

2.Not including the compat and simply using: app:showAsAction="ifRoom"/>

PS: Using Android Studio.

Should use android:showAsAction when not using the appcompat library

Issue: Ensures that menu items are using the right namespace Id: AppCompatResource

When using the appcompat library, menu resources should refer to the showAsAction in the app: namespace, not the android: namespace.

Similarly, when not using the appcompat library, you should be using the android:showAsAction attribute.

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

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:title="@string/action_refresh"
        android:icon="@drawable/toc"
        tools:ignore="AppCompatResource"
        compat:showAsAction="always"/> 
</menu>

All you need to do is add the following line to your item element to suppress the warning. Having the namespace as compat vs app for "http://schemas.android.com/apk/res-auto" doesn't actually do anything special.

tools:ignore="AppCompatResource"

full code:

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

    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:title="@string/action_refresh"
        android:icon="@drawable/toc"
        tools:ignore="AppCompatResource"
        compat:showAsAction="always"/>
</menu>

Note: the compat tag could be app or whatever you want.

I had the same issue in my multi-module Gradle project. It was reported by Android Lint CLI.

Environment of the issue

My modules look like:

我的模块

feature_home is an AppCompat module, which has a menu like:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/edit"
        android:icon="@drawable/ic_edit"
        android:title="@string/edit"
        app:showAsAction="always" />

</menu>

I wanted to run Android Lint for the entire application with a single ./gradlew command, so I added the following lint option to app/build.gradle :

lintOptions {
    checkDependencies true
}

Then, when I run ./gradlew :feature_home:lint , everything was fine, but when I tried to check all modules via ./gradlew :app:lint , I had the subject issue: Error: Should use android:showAsAction when not using the appcompat library .


Solution

The problem was due to my app module wasn't AppCompat . So adding the AppCompat dependency to app/build.gradle resolved the issue:

implementation "androidx.appcompat:appcompat:$appcompat_version"

For me woks this approach:

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

    <item
        android:id="@+id/maker_appbar_next"
        android:title="@string/next"
        app:showAsAction="always"
        tools:ignore="AppCompatResource" />

</menu>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM