You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遗留Android应用中Material3组件的样式/主题适配问题

问题:在Material2桥接主题应用中适配Material3 SearchBar/SearchView组件

环境与配置

  • Material组件依赖:
    implementation 'com.google.android.material:material:1.12.0'
    
  • 现有应用主题:基于Material2桥接主题,兼容AppCompat与Material2:
    <style name="Base.Theme.MyApp.NoActionBar" parent="Theme.MaterialComponents.NoActionBar.Bridge">
    

Fragment布局代码

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.search.SearchBar
            android:id="@+id/search_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/second_hint"
            app:backgroundTint="@color/dark"/>

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.search.SearchView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layout_anchor="@id/search_bar"
        android:hint="@string/second_hint"
        app:backgroundTint="@color/dark">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/search_list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

首次崩溃信息

加载Fragment时抛出布局解析异常,核心错误栈:

Caused by: android.view.InflateException: Binary XML file line #80 in com.myapp.android.debug:layout/mtrl_search_view: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f040346 a=-1}, theme={InheritanceMap=[id=0x7f1603bccom.myapp.android.debug:style/ThemeOverlay.Material3.Search], Themes=[com.myapp.android.debug:style/ThemeOverlay.Material3.Search, forced, com.myapp.android.debug:style/Theme.MyApp.NoActionBar, forced, com.myapp.android.debug:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f040346 a=-1}, theme={InheritanceMap=[id=0x7f1603bccom.myapp.android.debug:style/ThemeOverlay.Material3.Search], Themes=[com.myapp.android.debug:style/ThemeOverlay.Material3.Search, forced, com.myapp.android.debug:style/Theme.MyApp.NoActionBar, forced, com.myapp.android.debug:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}

错误指向Material库内部布局的这一行:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/materialSearchViewToolbarHeight"
    android:gravity="center_vertical"
    android:orientation="horizontal">

尝试的解决方法及新崩溃

为应用主题手动添加Material3相关样式属性:

<item name="materialSearchBarStyle">@style/Widget.Material3.SearchBar</item>
<item name="materialSearchViewStyle">@style/Widget.Material3.SearchView</item>
<item name="materialSearchViewPrefixStyle">@style/Widget.Material3.SearchView.Prefix</item>
<item name="materialSearchViewToolbarHeight">72dp</item>
<item name="materialSearchViewToolbarStyle">@style/Widget.Material3.SearchView.Toolbar</item>

之后出现新的布局解析崩溃:

Caused by: android.view.InflateException: Binary XML file line #118 in com.myapp.android.debug:layout/mtrl_search_view: Binary XML file line #118 in com.myapp.android.debug:layout/mtrl_search_view: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #118 in com.myapp.android.debug:layout/mtrl_search_view: Error inflating class <unknown>
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f040126 a=-1}, theme={InheritanceMap=[id=0x7f1603bccom.myapp.android.debug:style/ThemeOverlay.Material3.Search], Themes=[com.myapp.android.debug:style/ThemeOverlay.Material3.Search, forced, com.myapp.android.debug:style/Theme.MyApp.NoActionBar, forced, com.myapp.android.debug:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}

错误指向内部布局的分割线:

<View
    android:id="@+id/open_search_view_divider"
    android:layout_width="match_parent"
    android:layout_height="@dimen/m3_searchview_divider_size"
    android:background="@macro/m3_comp_search_view_divider_color"/>

即使直接给Fragment根布局设置@style/Theme.Material3.Dark样式,崩溃依然存在。

问题

如何让包含Material3 SearchBar/SearchView的新Fragment适配应用现有的Material2桥接主题?

内容的提问来源于stack exchange,提问作者kris larson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 02:00:22