【问题标题】:The style on this component requires your app theme to be Theme.AppCompat (or a descendant) : BottomNavigationView [duplicate]此组件的样式要求您的应用主题为 Theme.AppCompat(或后代):BottomNavigationView [重复]
【发布时间】:2021-05-09 14:36:55
【问题描述】:

com.google.android.material.bottomnavigation.BottomNavigationView 膨胀崩溃 开始出现在一些设备上。

崩溃日志:

Caused by java.lang.IllegalArgumentException
The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
com.google.android.material.internal.ThemeEnforcement.checkTheme (ThemeEnforcement.java:243)
com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme (ThemeEnforcement.java:213)
com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme (ThemeEnforcement.java:148)
com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes (ThemeEnforcement.java:115)
com.google.android.material.bottomnavigation.BottomNavigationView. (BottomNavigationView.java:160)
com.google.android.material.bottomnavigation.BottomNavigationView. (BottomNavigationView.java:133)

源代码:

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:elevation="8dp"
<.. app:itemIconTint and itemTextColor colors are set >
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

主题: 我的应用主题由 Theme.MaterialComponents.Light 扩展。文件(https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md#bridge-themes)说你应该使用 桥主题以支持材料组件。 我无法更改我的基本应用主题。 这是崩溃的原因吗?

所以我的问题是 -

我可以为 BottomnavigationView 定义自定义主题,它会扩展 Theme.MaterialComponents.Light.Bridge ..

 <style name="bottomNav" parent="Theme.MaterialComponents.Light.Bridge"> </style>

like this 并在 bottomNavigationView 中使用该样式

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navView"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@style/bottomNav"
/>

这会解决我的问题吗?为什么? 2. 还有其他方法吗?

【问题讨论】:

    标签: android material-design material-components-android


    【解决方案1】:

    您可以尝试在应用程序级别设置主题

     <application
            android:theme="@style/Theme.AppCompat"
    ...
     </application>
    

    【讨论】:

    • Theme.MaterialComponents.Light 只是 Theme.AppCompat.Light 的最终子项,目前我的应用程序主题是 Theme.MaterialComponents.Light。你也能解释一下吗?为什么它只出现在某些设备上(少于 5 %)?
    • 嗯,你用的是AndroidX吗?
    • 是的,使用AndroidX
    【解决方案2】:

    希望以下代码有所帮助

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:theme="@style/customTheme"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:itemTextColor="@drawable/selected_item_color"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav" />
    

    并将以下内容添加到themes.xml 文件中

    <style name="customTheme" parent="ThemeOverlay.AppCompat.Light">  
        <item name="android:textSize">13sp</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:fontFamily">@font/heebo_semibold</
     </style>
    

    【讨论】:

    • 我为什么要使用 ToolbarTheme 作为 BottomNavigationView ?为什么 ThemeOverlay ?我问的原因是,我无法重现这个,
    • 其实这只是一个名字(ToolbarTheme),你可以替换成任何名字。
    • 我正在使用带有底部导航的 createBadge,如果我移至 AppCompat 主题,它将停止工作,此外,我不清楚这将如何解决我的问题。
    • 你试过这个 style="@style/Widget.App.BottomNavigationView"
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2022-07-12
    • 2021-06-18
    • 2018-11-27
    • 2018-06-05
    • 2014-03-15
    相关资源
    最近更新 更多