java - 如何在布局中放置bottomAppBar

标签 java android material-components-android

我目前正在开发一个使用底部应用栏作为主要导航的项目。但即使我从示例源代码复制粘贴,我总是会收到错误

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"></com.google.android.material.bottomappbar.BottomAppBar>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

这是我的 gradle 依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

这是我遇到的错误

Binary XML file line #14: Binary XML file line #14: Error inflating class com.google.android.material.bottomappbar.BottomAppBar

最佳答案

根据this网站: 将您的 targetSdkVersion 设置为针对 Android P 的最新 API 版本(28),并确保您的应用继承 主题.MaterialComponents theme 以使 BottomAppBar 使用最新的样式。或者,您可以声明 BottomAppBar 的样式 在布局 xml 文件内的小部件声明中,如下所示: style="@style/Widget.MaterialComponents.BottomAppBar"

关于java - 如何在布局中放置bottomAppBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55784909/

相关文章:

java - 带有执行器的线程池的 POC(概念验证)

java - 具有多个 WebAppContext 实例的单点登录 Jetty

android Material 按钮无法去除圆角

android - 如何使用 Material 组件在 XML 中引用 `android:colorBackground`

java - Android Java - String .replaceAll 替换特定字符(正则表达式)

java - 为什么不在 LinkedList 中定义 ListIterator 的静态类?

android - invoke-direct 是否真的调用实例化对象中定义的方法?

安卓错误 : Only the original thread that created a view hierarchy can touch its views

java - RTSP 描述 header 但没有响应

android - ConstraintLayout 无法与 Android XML Preview 中的其他类一起实例化