Android Studio引用自定义Style报语法错误但项目可正常构建
If your custom styles work fine in the app but Android Studio throws false "extraneous input" errors and fails to track their references, try these solutions:
Move styles to
styles.xml
Google’s official documentation still recommends storing individual view styles instyles.xml, whilethemes.xmlis intended for app-wide themes that define default styles for components. Placing your styles in the correct file aligns with the IDE’s expectations and should eliminate parsing errors.Invalidate caches and restart Android Studio
Corrupted IDE cache is a common cause of false positive errors. Navigate toFile > Invalidate Caches... > Invalidate and Restartto clear stale data—this often fixes reference tracking issues as well.Verify syntax in
themes.xml
Even if the project builds successfully, subtle XML syntax issues (like missing closing tags, malformed attributes, or mixing theme and style definitions) can confuse the IDE’s lint checker. Double-check yourthemes.xmlfile for any such mistakes.Update Android Studio to the latest version
Older versions of Android Studio may have bugs with recognizing styles stored inthemes.xml. Updating to the latest stable release can resolve compatibility problems with the XML structure.Adjust IDE inspection settings
If the error persists, check if an overly strict XML inspection is triggering the false alert. Go toFile > Settings > Editor > Inspections > XML, look for inspections related to style or theme validation, and temporarily disable the one causing the "extraneous input" error.
内容的提问来源于stack exchange,提问作者Harley

