该组件的样式要求你的应用主题为theme.AppCompat(或其后代):BottomNavigationView.&l



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>

像这样的,并在底部导航视图

中使用这个样式
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navView"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@style/bottomNav"
/>

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

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

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

希望下面的代码对您有所帮助

<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>

最新更新