简体   繁体   中英

how to implement BottomAppBar in android?

how to implement BottomAppBar in android.

build.gradle

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

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

<!--    <FrameLayout-->
<!--        android:id="@+id/frames"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="wrap_content" />-->


    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        style="@style/Widget.MaterialComponents.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/bottom_app_bar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

when i start for implement BottomAppBar Then give me this error in xml file:

在此处输入图片说明

when i run app then Force stop app and give this error :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxxx.xxxx, PID: 8698
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.Activity.HomeActivity}: android.view.InflateException: Binary XML file line #14 in com.xxxx.xxxx:layout/activity_home2: Binary XML file line #14 in com.xxxx.xxxx:layout/activity_home2: Error inflating class com.google.android.material.bottomappbar.BottomAppBar
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3448)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7807)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
 Caused by: android.view.InflateException: Binary XML file line #14 in com.xxxx.xxxx:layout/activity_home2: Binary XML file line #14 in com.xxxx.xxxx:layout/activity_home2: Error inflating class com.google.android.material.bottomappbar.BottomAppBar
 Caused by: android.view.InflateException: Binary XML file line #14 in com.xxxx.xxxx:layout/activity_home2: Error inflating class com.google.android.material.bottomappbar.BottomAppBar

It's not an error - it's just view cannot be rendered by Android Studio, cause it doesn't know how to load this view. Moreover, you can build your application without any errors. If you want make preview working - try click rebuild in Build settings of Android Studio. It should help to render a preview.

Update your theme of MaterialComponents

<style name="AppTheme" parent="Theme.MaterialComponents.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>

The latest version of Material Library is given here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM