Android 4中如何使用NavigationView

How to use NavigationView in Android 4

在我的应用程序中,我使用了 NavigationView,当 Android 上的 运行 应用程序时,4 显示强制关闭错误,但在 android 5 及以上没有显示任何错误并最终工作!

我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/mainPage_drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--Root-->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_main_gradient"
        tools:context=".ui.main.activity.MainPage">
        <!--Toolbar-->
        <com.github.florent37.shapeofview.shapes.ArcView
            android:id="@+id/mainPage_toolbar"
            android:layout_width="0dp"
            android:layout_height="@dimen/_60mdp"
            android:elevation="@dimen/_2mdp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shape_arc_height="@dimen/_10mdp"
            app:shape_arc_position="bottom">
            <!--Content-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/_60mdp"
                android:background="@android:color/white"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">
                <!--Search-->
                <com.app.app.utils.custom_view.FontAwesomeTextView
                    android:id="@+id/homePage_toolbarSearch"
                    style="@style/fontAwesome"
                    android:layout_width="@dimen/_25mdp"
                    android:layout_height="@dimen/_25mdp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/_10mdp"
                    android:text="@string/awesomeSearch"
                    android:textColor="@color/toolbarDarkGray"
                    android:textSize="@dimen/_18font_mdp" />
                <!--Title-->
                <TextView
                    android:id="@+id/homePage_toolbarTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="@string/home"
                    android:textColor="@color/toolbarDarkGray"
                    android:textSize="@dimen/_16font_mdp" />
                <!--Menu-->
                <com.app.app.utils.custom_view.FontAwesomeTextView
                    android:id="@+id/homePage_toolbarMenu"
                    style="@style/fontAwesome"
                    android:layout_width="@dimen/_25mdp"
                    android:layout_height="@dimen/_25mdp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="@dimen/_10mdp"
                    android:text="@string/awesomeMenu"
                    android:textColor="@color/toolbarDarkGray"
                    android:textSize="@dimen/_18font_mdp" />

            </RelativeLayout>

        </com.github.florent37.shapeofview.shapes.ArcView>
        <!--ViewPager-->
        <com.aurelhubert.ahbottomnavigation.AHBottomNavigationViewPager
            android:id="@+id/mainPage_viewPager"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@id/mainPage_bottomNavigation"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="@dimen/_40mdp"/>
        <!--BottomNavBar-->
        <com.aurelhubert.ahbottomnavigation.AHBottomNavigation
            android:id="@+id/mainPage_bottomNavigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:selectedBackgroundVisible="false" />

    </androidx.constraintlayout.widget.ConstraintLayout>
    <!--Navigation view-->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/mainPage_navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@drawable/bg_menu_gradient"
        android:layoutDirection="rtl"
        app:headerLayout="@layout/header_nav_menu"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/main_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

ForeClose 错误消息:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.app/com.app.app.ui.main.activity.MainPage}: android.view.InflateException: Binary XML file line #93: Error inflating class com.google.android.material.navigation.NavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
        at android.app.ActivityThread.access0(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5019)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #93: Error inflating class com.google.android.material.navigation.NavigationView
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater.createCustomViewInternal(ViewPumpLayoutInflater.java:204)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater.access[=12=]0(ViewPumpLayoutInflater.java:14)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater$PrivateWrapperFactory2ViewCreator.onCreateView(ViewPumpLayoutInflater.java:405)
        at io.github.inflationx.viewpump.FallbackViewCreationInterceptor.intercept(FallbackViewCreationInterceptor.java:11)
        at io.github.inflationx.viewpump.InterceptorChain.proceed(InterceptorChain.java:37)
        at io.github.inflationx.calligraphy3.CalligraphyInterceptor.intercept(CalligraphyInterceptor.java:18)
        at io.github.inflationx.viewpump.InterceptorChain.proceed(InterceptorChain.java:37)
        at io.github.inflationx.viewpump.ViewPump.inflate(ViewPump.java:49)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater$PrivateWrapperFactory2.onCreateView(ViewPumpLayoutInflater.java:385)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater.inflate(ViewPumpLayoutInflater.java:55)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.app.app.ui.main.activity.MainPage.onCreate(MainPage.java:94)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243) 
        at android.app.ActivityThread.access0(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5019) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:594)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater.createCustomViewInternal(ViewPumpLayoutInflater.java:204) 
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater.access[=12=]0(ViewPumpLayoutInflater.java:14) 
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater$PrivateWrapperFactory2ViewCreator.onCreateView(ViewPumpLayoutInflater.java:405) 
        at io.github.inflationx.viewpump.FallbackViewCreationInterceptor.intercept(FallbackViewCreationInterceptor.java:11) 
        at io.github.inflationx.viewpump.InterceptorChain.proceed(InterceptorChain.java:37) 
        at io.github.inflationx.calligraphy3.CalligraphyInterceptor.intercept(CalligraphyInterceptor.java:18) 
        at io.github.inflationx.viewpump.InterceptorChain.proceed(InterceptorChain.java:37) 
        at io.github.inflationx.viewpump.ViewPump.inflate(ViewPump.java:49) 
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater$PrivateWrapperFactory2.onCreateView(ViewPumpLayoutInflater.java:385) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater.inflate(ViewPumpLayoutInflater.java:55) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
        at com.app.app.ui.main.activity.MainPage.onCreate(MainPage.java:94) 
        at android.app.Activity.performCreate(Activity.java:5231) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243) 
        at android.app.ActivityThread.access0(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5019) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at io.github.inflationx.viewpump.ViewPumpLayoutInflater$NameAndAttrsViewCreator.onCreateView(ViewPumpLayoutInflater.java:276)
        at io.github.inflationx.viewpump.FallbackViewCreationInterceptor.intercept(FallbackViewCreationInterceptor.java:11)
        at io.github.inflationx.viewpump.InterceptorChain.proceed(InterceptorChain.java:37)
        at io.github.inflationx.calligraphy3.CalligraphyInterceptor.intercept(CalligraphyInterceptor.java:18)
        at io.github.infl

同时将此 vectorDrawables.useSupportLibrary = true 添加到 build.gradle 文件

defaultConfig

更新

header_nav_menu 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layoutDirection="ltr"
    android:layout_height="@dimen/_100mdp">
    <!--Oval shape-->
    <RelativeLayout
        android:id="@+id/headerMainMenu_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/mines50mdp"
        android:layout_marginBottom="@dimen/_10mdp"
        android:background="@drawable/bg_menu_header_oval"
        android:scaleX="1.5"
        android:scaleY="1.5" />
    <!--Close-->
    <com.app.app.utils.custom_view.FontAwesomeTextView
        android:id="@+id/headerMainMenu_close"
        style="@style/fontAwesome"
        android:layout_width="@dimen/_25mdp"
        android:layout_height="@dimen/_25mdp"
        android:layout_marginLeft="@dimen/_10mdp"
        android:layout_marginTop="@dimen/_5mdp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/awesomeClose"
        android:textColor="@color/toolbarDarkGray"
        android:textSize="@dimen/_20font_mdp" />
    <!--Not login-->
    <LinearLayout
        android:id="@+id/headerMenu_notLoginLay"
        android:layout_width="match_parent"
        android:layout_toRightOf="@+id/headerMainMenu_close"
        android:layout_height="match_parent"
        android:gravity="right"
        android:orientation="vertical">
        <!--Title-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_6mdp"
            android:layout_marginRight="@dimen/_10mdp"
            android:text="@string/guestUser"
            android:textColor="@android:color/black"
            android:textSize="@dimen/_16font_mdp"
            android:textStyle="bold" />
        <!--Desc-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_2mdp"
            android:layout_marginRight="@dimen/_10mdp"
            android:text="@string/clickToLogin"
            android:textColor="@android:color/black"
            android:textSize="@dimen/_12font_mdp" />
        <!--Login-->
        <com.app.app.utils.custom_view.reounded_textview.RoundTextView
            android:id="@+id/headerMenu_notLoginBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10mdp"
            android:layout_marginRight="@dimen/_10mdp"
            android:elevation="@dimen/_2mdp"
            android:gravity="center"
            android:paddingLeft="@dimen/_25mdp"
            android:paddingTop="@dimen/_3mdp"
            android:paddingRight="@dimen/_25mdp"
            android:paddingBottom="@dimen/_5mdp"
            android:text="@string/login"
            android:textColor="@android:color/white"
            android:textSize="@dimen/_12font_mdp"
            app:allCorner="@dimen/_15mdp"
            app:bgColor="@android:color/black" />

    </LinearLayout>
    <!--Login-->
    <LinearLayout
        android:id="@+id/headerMenu_loginLay"
        android:layout_width="match_parent"
        android:layout_toRightOf="@+id/headerMainMenu_close"
        android:layout_height="match_parent"
        android:gravity="right"
        android:visibility="gone"
        android:orientation="vertical">
        <!--Title-->
        <TextView
            android:id="@+id/headerMenu_loginTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_6mdp"
            android:layout_marginRight="@dimen/_10mdp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/_16font_mdp"
            android:textStyle="bold" />
        <!--Desc-->
        <TextView
            android:id="@+id/headerMenu_loginUserCredit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_2mdp"
            android:layout_marginRight="@dimen/_10mdp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/_12font_mdp" />
        <!--Login-->
        <com.app.app.utils.custom_view.reounded_textview.RoundTextView
            android:id="@+id/headerMenu_increaseCreditBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10mdp"
            android:layout_marginRight="@dimen/_10mdp"
            android:elevation="@dimen/_2mdp"
            android:gravity="center"
            android:paddingLeft="@dimen/_25mdp"
            android:paddingTop="@dimen/_3mdp"
            android:paddingRight="@dimen/_10mdp"
            android:paddingBottom="@dimen/_3mdp"
            android:text="@string/increaseCredit"
            android:drawableRight="@drawable/ic_add_white"
            android:drawableEnd="@drawable/ic_add_white"
            android:drawablePadding="@dimen/_5mdp"
            android:textColor="@android:color/white"
            android:textSize="@dimen/_10font_mdp"
            app:allCorner="@dimen/_15mdp"
            app:bgColor="@android:color/black" />

    </LinearLayout>

</RelativeLayout>

如何解决 android 4 上的这个问题?

从您的代码中删除此行

        android:drawableRight="@drawable/ic_add_white"
        android:drawableEnd="@drawable/ic_add_white"