我正在尝试使用我的应用程序中材料组件库中的
SearchBar
和 SearchView
。 这些是 Material 3 的新组件。
implementation 'com.google.android.material:material:1.12.0'
这是一个已经开发了很长时间的应用程序,因此主题使用“bridge”主题,以便 AppCompat 可以继续与 Material 2 一起工作:
<style name="Base.Theme.MyApp.NoActionBar" parent="Theme.MaterialComponents.NoActionBar.Bridge">
我有一个看起来像这样的片段:
<?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>
膨胀此片段会产生以下崩溃:
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]}
第 80 行位于此视图的底部:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/materialSearchViewToolbarHeight"
android:gravity="center_vertical"
android:orientation="horizontal">
我认为它对属性引用感到窒息。
所以我将此块添加到样式中:
<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"/>
如果我将 Material3 主题直接应用于片段:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Theme.Material3.Dark"
我也遇到同样的崩溃。
我想我不太了解Android主题。
如何使这个带有 Material 3 组件的新片段符合我的应用程序的风格?