我正在尝试向我的活动添加 BottomAppBar,但出现错误:" java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.mathmech.cards/com.mathmech.cards.activity.MainActivity}: android.view.InflateException: Binary XML 文件行 #24: 二进制 XML 文件行 #24: 膨胀类 com.google.android.material.bottomappbar.BottomAppBar 时出错">
我试图遵循网络上的指南,但没有任何帮助
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activity.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
app:fabAlignmentMode="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:gravity="bottom" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
app:layout_anchor="@id/bottom_app_bar"
android:layout_width="wrap_content"
android:src="@drawable/ic_add_black_24dp"
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
编辑:添加样式.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
不要使用父主题作为Theme.AppCompat.Light.DarkActionBar
,而是使用 Theme.MaterialComponents
或其后代。
尝试在底部应用栏 XML 中使用此样式。
style=”@style/Widget.MaterialComponents.BottomAppBar”
是的,将Theme.AppCompact.Light.DarkActionBar
更改为Theme.MaterialComponents
将帮助您在布局编辑器中查看 xml。
另外,您可以关注本文实现底部应用栏 I:适用于 Android 的材质组件