【问题标题】:Navigationview and DrawerLayout can not resolveNavigationview 和 DrawerLayout 无法解析
【发布时间】:2018-04-19 09:20:52
【问题描述】:
我正在尝试在代码中使用DrawerLayout 对象或NavigationView 对象,但android studio 无法解析。
这是Build.gradle
我正在使用安卓工作室3.1.1
dependencies {
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:2.2.2'
}
我会感谢任何帮助
【问题讨论】:
标签:
android
drawerlayout
navigationview
【解决方案1】:
您必须添加support-v4 库,您只导入support 库
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
确保您将其导入以使用 NavigationDrawer
import android.support.design.widget.NavigationView;
确保您将其导入以使用 DrawerLayout
import android.support.v4.widget.DrawerLayout;
【解决方案2】:
您需要将此依赖项添加到您的 gradle 文件中:
'com.google.android.material:material:1.1.0'
【解决方案3】:
你需要包含
compile "com.android.support:appcompat-v7:21.0.+"
compile "com.android.support:appcompat-v4:21.0.+"
compile "com.android.support:design:21.0.+"
使用这些依赖项的最新版本
【解决方案4】:
确保您的 AppCompat 库和设计支持库在 Gradle 中具有相同的版本。
除此之外,如果您使用的是 androidX,您的设计库将如下所示
implementation 'com.google.android.material:material:{Your-Version}'
你需要使用这个调用 NavigationView
<com.google.android.material.navigation.NavigationView
...
/>
而不是旧的设计支持库调用,即
<android.support.design.widget.NavigationView
...
/>