Как разместить bottomAppBar в макете

1

В настоящее время я работаю над проектом, который использует нижнюю панель приложения в качестве основной навигации. но я всегда получаю сообщение об ошибке, даже когда копирую из исходного кода примера

Вот мой файл макета

<?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
Теги:
material

2 ответа

0

Я решил эту проблему недавно. Вам нужно добавить зависимость

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>
  • 0
    Спасибо, я только что нашел решение прошлой ночью. Моя проблема в том, что текущая версия зависимости все еще альфа и не стабильна. Я использовал версию материала '1.1.0-alpha05', поэтому я понизил его до 1.0.0 и все работает нормально и гладко
0

В соответствии с этим веб-сайтом: установите для targetSdkVersion последнюю версию API для Android P, значение которой равно 28, и убедитесь, что ваше приложение наследует тему Theme.MaterialComponents, чтобы BottomAppBar использовал последний стиль. Кроме того, вы можете объявить стиль для BottomAppBar в объявлении виджета в файле макета XML следующим образом: style="@style/Widget.MaterialComponents.BottomAppBar"

Ещё вопросы

Сообщество Overcoder
Наверх
Меню