android - 如何修复 FAB 内部活动?
问题描述
我正在尝试将 BottomAppBar 添加到我的活动中,但出现错误:“java.lang.RuntimeException:无法启动活动 ComponentInfo{com.mathmech.cards/com.mathmech.cards.activity.MainActivity}:android.view。 InflateException:二进制 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>
编辑:添加了styles.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
或其后代。
推荐阅读
- docker - Docker多阶段不调用入口点
- wordpress - 收到一定数量的视图后如何在 WordPress 上隐藏内容
- python - 使用Django、python和vue axiox提交表单后重定向到主页
- python - 我应该将数据库连接或游标传递给一个类
- mysql - 由于阻塞导致的 Laravel 连接数据库问题
- regex - 使用正则表达式排除字符组合或添加字母
- google-cloud-run - 无法使用来自另一个项目的映像部署 Cloud Run 服务
- c# - 当第二层在第一层和第三层之间时,如何避免它们之间的碰撞?Unity 2D C#
- python - 为什么 con 定义中没有“设置”图标
- python - python pandas 在最后一个非 NaN 值处停止 fillna