簡體   English   中英

如何在布局中放置bottomAppBar

[英]How to place bottomAppBar in layout

我目前正在開發一個使用底部應用欄作為其主要導航的項目。 但即使我從示例源代碼中復制粘貼,我也總是收到錯誤消息

這是我的布局文件

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

根據網站:將您的 targetSdkVersion 設置為針對 Android P 的最新 API 版本,即 28,並確保您的應用繼承Theme.MaterialComponents主題,以使 BottomAppBar 使用最新樣式。 或者,您可以在布局 xml 文件中的小部件聲明中聲明BottomAppBar的樣式,如下所示: style="@style/Widget.MaterialComponents.BottomAppBar"

我最近解決了這個問題。 你需要添加依賴

implementation 'com.google.android.material:material:1.0.0

然后將<com.google.android.material.bottomnavigation.BottomNavigationView添加到您的 XML 布局文件中。

它對我有用

<?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" android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_height="wrap_content" android:id="@+id/vBottomBar"
            app:layout_constraintHorizontal_bias="0.0"/>
    <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/vBottomBar" android:id="@+id/vContainer">

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我昨晚找到了解決我的問題的方法。 我的問題是我使用

implementation 'com.google.android.material:material:1.1.0-alpha05'

並且那個版本仍然是 alpha 並且不穩定。 所以我把它降級為

implementation 'com.google.android.material:material:1.0.0'

一切都像黃油一樣順暢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM