Android Studio更新后XML文件资源编译错误求助
Hey there, let's tackle these resource errors you're seeing—they're pretty common after updating Android Studio, especially since newer versions lean into AndroidX instead of the old support libraries. Here's how to fix each issue step by step:
1. android:contentScrim 属性未找到
The contentScrim attribute belongs to the Material Components library (formerly the Design Support Library), so it needs to use the app namespace instead of android. Change:
android:contentScrim="?attr/colorPrimary"
to:
app:contentScrim="?attr/colorPrimary"
2. attr/actionBarSize 与 android:layout_height 不兼容
You're missing the leading ? that tells Android this is a theme attribute reference. Update your Toolbar's layout_height line to:
android:layout_height="?attr/actionBarSize"
If you migrate to AndroidX later, you can also use ?androidx.appcompat.R.attr.actionBarSize for even better compatibility, but the above works for both support and AndroidX setups.
3. android:srcCompact 属性未找到
srcCompact (now standardized as srcCompat) is an attribute for AppCompatImageView and requires the app namespace, not android. For every instance of android:srcCompact, replace it with:
app:srcCompat="@drawable/your_drawable_name"
Make sure you have the AppCompat dependency in your module-level build.gradle to use this:
implementation 'androidx.appcompat:appcompat:1.6.1'
Bonus: Migrate to AndroidX (Highly Recommended)
Since you updated Android Studio, fully migrating to AndroidX will eliminate most future compatibility headaches. Here's how:
- Go to
Refactor > Migrate to AndroidX...in Android Studio - Follow the prompts to back up your project and apply the migration
- This automatically updates all old support library references to their AndroidX equivalents
Corrected XML Code Snippet
Here's your activity_main6.xml with all fixes applied (including AndroidX migration updates):
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.kara.kavin.myapplication.Main6Activity"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="300dp" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collasingtoolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="exitUntilCollapsed|scroll" app:contentScrim="?attr/colorPrimary" app:title="NEAT Patrol" app:expandedTitleMarginStart="48dp" app:expandedTitleMarginEnd="64dp" android:background="@drawable/sam"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbarid" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_collapseMode="pin" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" android:background="#fcfcfc" android:gravity="center" android:id="@+id/ll"> <LinearLayout android:clipToPadding="false" android:gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.cardview.widget.CardView android:foreground="?android:attr/selectableItemBackground" android:clickable="true" android:id="@+id/bankcardId" android:layout_width="160dp" android:layout_height="190dp" android:layout_margin="10dp"> <!-- Insert your LinearLayout content here --> </androidx.cardview.widget.CardView> </LinearLayout> </LinearLayout> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
After making these changes, clean and rebuild your project (Build > Clean Project then Build > Rebuild Project) to resolve the :app:processDebugResources failure.
内容的提问来源于stack exchange,提问作者kara

