实现Collapsing Toolbar Layout遇contentScrim属性未找到错误求助
解决
attribute 'package:contentScrim' not found错误 这个报错大概率是依赖库或控件兼容性问题,咱们一步步搞定:
检查并更新依赖库
如果还在使用旧的Support库,建议直接迁移到AndroidX(官方已停止维护Support库),在Module级别的build.gradle里添加Material库依赖:implementation 'com.google.android.material:material:1.9.0'要是坚持用旧Support库,确保添加对应版本的Design库:
implementation 'com.android.support:design:28.0.0'替换为对应版本的控件类
迁移到AndroidX后,XML里的控件需要换成AndroidX的类名,比如:android.support.design.widget.CollapsingToolbarLayout→com.google.android.material.appbar.CollapsingToolbarLayoutandroid.support.design.widget.CoordinatorLayout→com.google.android.material.coordinatorlayout.CoordinatorLayout
你的XML里已经正确声明了app命名空间,这部分没问题。
简易实现Fragment视差粘性头部
下面给你一个基于AndroidX的完整简化版实现,包含视差头部、粘性Toolbar、TabLayout+ViewPager+Fragment的组合:
1. 主布局文件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <com.google.android.material.coordinatorlayout.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="256dp" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false"> <!-- 视差滚动的头部图片 --> <ImageView android:id="@+id/header_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/header" app:layout_collapseMode="parallax"/> <!-- 图片半透明遮罩,提升文字可读性 --> <View android:layout_width="match_parent" android:layout_height="match_parent" android:alpha="0.3" android:background="@android:color/black"/> <!-- 粘性固定的Toolbar --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="top" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"/> <!-- 底部固定的TabLayout --> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:tabIndicatorColor="@android:color/white" app:tabSelectedTextColor="@android:color/white" app:tabTextColor="@color/white_70"/> </com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.AppBarLayout> <!-- 承载Fragment的ViewPager,需绑定滚动行为 --> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </com.google.android.material.coordinatorlayout.CoordinatorLayout>
2. Activity逻辑代码(MainActivity.kt)
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.viewpager.widget.ViewPager import com.google.android.material.tabs.TabLayout class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 设置Toolbar为ActionBar setSupportActionBar(findViewById(R.id.toolbar)) supportActionBar?.setDisplayHomeAsUpEnabled(true) // 初始化ViewPager和TabLayout val viewPager = findViewById<ViewPager>(R.id.view_pager) val tabLayout = findViewById<TabLayout>(R.id.tab_layout) // 创建Pager适配器,替换成你的Fragment列表 val adapter = ViewPagerAdapter(supportFragmentManager) adapter.addFragment(HomeFragment(), "首页") adapter.addFragment(DiscoverFragment(), "发现") adapter.addFragment(ProfileFragment(), "我的") viewPager.adapter = adapter tabLayout.setupWithViewPager(viewPager) } }
3. ViewPager适配器代码
import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter class ViewPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { private val fragmentList = mutableListOf<Fragment>() private val titleList = mutableListOf<String>() fun addFragment(fragment: Fragment, title: String) { fragmentList.add(fragment) titleList.add(title) } override fun getItem(position: Int): Fragment = fragmentList[position] override fun getCount(): Int = fragmentList.size override fun getPageTitle(position: Int): CharSequence? = titleList[position] }
4. 示例Fragment(HomeFragment.kt)
import androidx.fragment.app.Fragment import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView class HomeFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fragment_home, container, false) view.findViewById<TextView>(R.id.content_text).text = "首页Fragment内容,滚动这里可以触发头部视差效果" return view } }
5. Fragment布局(fragment_home.xml)
<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/content_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:textSize="18sp" android:lineSpacingExtra="8dp"/> </androidx.core.widget.NestedScrollView>
核心效果说明:
app:layout_collapseMode="parallax":给头部图片添加视差滚动效果,滚动时图片移动速度慢于Toolbar,营造层次感。app:layout_collapseMode="pin":让Toolbar固定在顶部,滚动时不会被收起。app:layout_scrollFlags="scroll|exitUntilCollapsed":控制头部的滚动行为,向上滚动时收起头部区域,直到Toolbar完全显示并固定。- 必须使用可滚动的控件(如
NestedScrollView、RecyclerView)作为Fragment的根布局,才能触发AppBarLayout的滚动交互。
内容的提问来源于stack exchange,提问作者sanjay
相关产品推荐
相关产品推荐

