【问题标题】:How to fix FAB inside activity?如何修复 FAB 内部活动?
【发布时间】:2019-05-28 17:19:57
【问题描述】:

我正在尝试将 BottomAppBar 添加到我的活动中,但出现错误:“ java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mathmech.cards/com.mathmech.cards.activity.MainActivity}: android .view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class 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>

【问题讨论】:

  • 你是否为BottomAppBar添加了正确的依赖?
  • @SaurabhThorat 依赖项 { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'androidx.appcompat:appcompat:1.1.0-alpha05' implementation 'androidx.constraintlayout :constraintlayout:2.0.0-beta1' testImplementation 'junit:junit:4.13-beta-3' androidTestImplementation 'androidx.test:runner:1.2.0-beta01' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0 -beta01' 实施 'org.jetbrains:annotations:15.0' 实施 'com.google.android.material:material:1.1.0-alpha06' }
  • 发布您的styles.xml
  • @SaurabhThorat 在这里(已编辑帖子)
  • 发布你的 build.gradle

标签: android xml


【解决方案1】:

不要将您的父主题用作Theme.AppCompat.Light.DarkActionBar,而是使用Theme.MaterialComponents 或其后代。

【讨论】:

    【解决方案2】:

    在底部应用栏 XML 中尝试使用这种样式。

    style=”@style/Widget.MaterialComponents.BottomAppBar”
    

    【讨论】:

      【解决方案3】:

      是的,将 Theme.AppCompact.Light.DarkActionBar 更改为 Theme.MaterialComponents 将有助于您在布局编辑器中查看 xml。

      另外,你可以关注这篇文章 Implementing BottomAppBar I: Material Components for Android

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-15
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 2018-07-28
        • 1970-01-01
        • 1970-01-01
        • 2019-10-23
        相关资源
        最近更新 更多