1. 程式人生 > >完美解決 use app:showAsAction when not using the appcompat library

完美解決 use app:showAsAction when not using the appcompat library

因為使用了showAsAction,所以在以下程式碼中,會導致如題的異常提示。

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        android:icon="@drawable/icon_feature_setting"
        android:showAsAction
="ifRoom" />
</menu>

解決方法:

加入 xmlns:app=”http://schemas.android.com/tools” 和把android:showAsAction 改為 app:showAsAction 。
即可解決此問題。

完整程式碼:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
>
<item android:id="@+id/action_settings" android:icon="@drawable/icon_feature_setting" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="ifRoom" /> </menu>

若在Android Studio中,加入修改之後的程式碼後,還是有提示錯誤的話, 在app:showAsAction上按Alt+Enter匯入必要資訊,即可解決啦