安卓NavigationView未找到。

3

I have the following xml:

<android.support.v4.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/container"
        />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
       />
</android.support.v4.widget.DrawerLayout>

但是我遇到了错误:no such class android.support.design.widget.NavigationView

以下是我的Gradle中的SDK配置:

 minSdkVersion 16
    targetSdkVersion 23

出于安全原因,我的项目不允许使用jcenter或远程仓库,因此我必须始终将依赖项作为jar文件包含在项目中。如何解决这个错误?

4个回答

3

您出现这个错误是因为需要使用Design Support Library

在SDK Manager中下载Android Support Repository,然后您可以在此处找到库的最新版本:

<android-sdk>/extras/android/m2repository/com/android/support/design/23.0.1/design-23.0.1.aar

把它复制到你的libs目录下,然后将其添加为你的build.gradle的依赖项:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile 'android.support.design:design:23.0.1@aar'
}

1
你的Gradle依赖中有两个design?你把单词design写了两次,这样正确吗? - j2emanue
是的,在冒号之前的第一个字符串是包名。您也可以像这样留空compile ':design:23.0.1@aar'。重要的部分是第二部分和标识文件的版本号。 - Mattia Maestrini
我不建议直接复制AAR。请参考我的答案:https://dev59.com/YI_ea4cB1Zd3GeqPT9wS#37638689 - boltup_im_coding
通常我不建议直接复制AAR文件,但是OP要求提供一种包含库文件的方法:“由于安全原因,我不能在我的项目中使用jcenter或远程存储库,因此我必须始终将依赖项作为jar文件包含在内。” - Mattia Maestrini

1

进入Gradle。在Build文件中点击构建图标,选择添加依赖项/library,然后点击Design Library,最后同步。 确保添加AppCompat库以确保您的应用程序具有向后兼容性。


支持存储库有新的更新,请确保您拥有最新版本以保持安全。 - Samuel Owino

0
SDK 管理器 中,确保你已经下载了 Android 支持库
然后,在你的 build.gradle 文件中添加以下内容。
compile 'com.android.support:design:23.4.0'

0
在添加依赖项后,在布局文件中使用:
com.google.android.material.navigation.NavigationView 

针对 androidx 上的 navigationView


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接