android迁移到androidx报错 AAPT: error: attribute headerLayout (aka com.example.phonehelper:headerLayout) not found.

1.报错详情:在这里插入图片描述

2.报错原因

<?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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>

    <!--<com.google.android.material.navigation.NavigationView
        app:headerLayout="@layout/layout_header"
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_left"
       >

    </com.google.android.material.navigation.NavigationView>-->
    
    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/layout_header"
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/menu_left">

    </android.support.design.widget.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

​ 今天打算使用androidx.drawerlayout.widget.DrawerLayout实现一个侧滑菜单,由于教程比较旧,原来的布局文件已经不能使用,只好自行升级。由于在<androidx.drawerlayout.widget.DrawerLayout>标签内使用了<android.support.design.widget.NavigationView>标签,导致不兼容,无法找到目标资源文件。

3.解决方法

3.1添加依赖

在build.gradle(:app)下添加依赖

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

关于android迁移到androidx的映射可参照这篇 博客

也可以参考官方文档: 官方文档

3.2修改布局

在这里插入图片描述

将上面方框的注释撤销,将下面方框的代码注释掉,完成布局

4.运行效果
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐