在 Android 開發中,許多開發者會遇到這樣的困擾:別人的自訂屬性(如 app:xxx)在 XML 編輯時能正常彈出智能提示,但自己定義的命名空間卻完全沒有反應,甚至連 Alt+/ 也無法觸發代碼補全。
xmlns:app="https://schemas.android.com/apk/res-auto" 當成普通字串,忽略其特殊含義關鍵在於命名空間 URI 的寫法。 Android 工具鏈只識別標準的 https://schemas.android.com/apk/res-auto 這一個 URI。當你使用這個固定 URI 時,IDE 會自動解析模組中的 attrs.xml,從而提供屬性提示。若自行定義其他 URI(如 https://schemas.android.com/apk/res/com.example.app),IDE 將無法關聯到資源,自然也就沒有提示。
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
... >
<!-- 這裡的 app:xxx 就會有智能提示 -->
</LinearLayout>
記住:永遠使用 https://schemas.android.com/apk/res-auto 作為自訂屬性的命名空間 URI。這不是隨意可改的字串,而是 Android 建構系統約定的協議。包名一致只是前提,正確的 URI 才是啟用提示的關鍵。