添加Navigation Drawer后Activity内控件无法响应点击的问题
在原本正常运行的Activity中添加了Navigation Drawer,将顶部部分按钮移入其中。目前Navigation Drawer的滑动、点击监听功能正常,点击后可关闭,但Activity内所有控件均无法响应点击操作。
相关布局代码
主Activity布局
<RelativeLayout 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="wrap_content" android:layout_height="match_parent" android:background="#fff" tools:context="com.example.fooddelivery.Activities.MainAdminActivity"> <RelativeLayout android:id="@+id/toolBarRL" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btn_grad" android:padding="10dp" > <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profileIv" android:layout_width="80dp" android:layout_height="80dp" android:visibility="gone" android:src="@drawable/ic_person_grey" app:civ_border_color="@color/orange" app:civ_border_overlay="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@+id/profileIv" android:layout_marginTop="50dp" android:background="@drawable/shape_rec03" android:orientation="horizontal" android:padding="0dp"> <!--Service Providers--> <TextView android:id="@+id/tabSPTv" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_weight="1" android:background="@drawable/shape_rec04" android:gravity="center" android:text="Service Providers" android:textColor="#000000" /> <!--Orders--> <TextView android:id="@+id/tabUsersTv" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_weight="1" android:gravity="center" android:text="Users" android:textColor="#ffffff" /> </LinearLayout> </RelativeLayout> <!--Service providers UI--> <RelativeLayout android:id="@+id/SPRL" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:layout_below="@+id/toolBarRL"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/shopsRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginBottom="5dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" tools:listitem="@layout/row_shop" /> </RelativeLayout> <RelativeLayout android:id="@+id/usersRl" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:layout_below="@+id/toolBarRL"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/usersRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginBottom="5dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:reverseLayout="true" app:stackFromEnd="true" tools:listitem="@layout/row_all_users" /> </RelativeLayout> <include layout="@layout/navigation_drawer_activity" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
Header布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="@color/orange" android:padding="15dp" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_person_grey" android:contentDescription="Header image" android:id="@+id/profileIv" android:layout_margin="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" android:id="@+id/nameTv" android:textColor="#fff" android:textSize="20sp" android:layout_margin="0dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:text="Email" android:id="@+id/emailTv" android:layout_margin="10dp" android:textSize="12sp" /> <TextView android:id="@+id/phoneTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white" android:textSize="10sp" android:text="0321-1234567" /> </LinearLayout>
Toolbar布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/appL"> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/orange" android:id="@+id/toolbar" /> </com.google.android.material.appbar.AppBarLayout> </LinearLayout>
Navigation Drawer布局
<?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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:id="@+id/drawer" tools:openDrawer="start" tools:context="com.example.fooddelivery.Activities.MainAdminActivity"> <include layout="@layout/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:id="@+id/navView" app:headerLayout="@layout/header" app:menu="@menu/navigation_seller_main"> </com.google.android.material.navigation.NavigationView> </androidx.drawerlayout.widget.DrawerLayout>
解决方案
问题核心是布局层级错误:你把DrawerLayout作为子布局嵌套到主Activity的RelativeLayout中,且DrawerLayout宽高设为match_parent,完全覆盖了下方所有控件,导致这些控件无法接收点击事件。
正确做法是将DrawerLayout作为根布局,把原Activity的所有内容放到DrawerLayout的主内容区域(与NavigationView同级),调整后的主Activity布局如下:
<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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:id="@+id/drawer" tools:openDrawer="start" tools:context="com.example.fooddelivery.Activities.MainAdminActivity"> <!-- 主内容区域:原Activity的所有内容 --> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff"> <RelativeLayout android:id="@+id/toolBarRL" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btn_grad" android:padding="10dp" > <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profileIv" android:layout_width="80dp" android:layout_height="80dp" android:visibility="gone" android:src="@drawable/ic_person_grey" app:civ_border_color="@color/orange" app:civ_border_overlay="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@+id/profileIv" android:layout_marginTop="50dp" android:background="@drawable/shape_rec03" android:orientation="horizontal" android:padding="0dp"> <!--Service Providers--> <TextView android:id="@+id/tabSPTv" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_weight="1" android:background="@drawable/shape_rec04" android:gravity="center" android:text="Service Providers" android:textColor="#000000" /> <!--Orders--> <TextView android:id="@+id/tabUsersTv" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_weight="1" android:gravity="center" android:text="Users" android:textColor="#ffffff" /> </LinearLayout> </RelativeLayout> <!--Service providers UI--> <RelativeLayout android:id="@+id/SPRL" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:layout_below="@+id/toolBarRL"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/shopsRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginBottom="5dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" tools:listitem="@layout/row_shop" /> </RelativeLayout> <RelativeLayout android:id="@+id/usersRl" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:layout_below="@+id/toolBarRL"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/usersRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginBottom="5dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:reverseLayout="true" app:stackFromEnd="true" tools:listitem="@layout/row_all_users" /> </RelativeLayout> </RelativeLayout> <!-- NavigationView 侧滑菜单 --> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" android:id="@+id/navView" app:headerLayout="@layout/header" app:menu="@menu/navigation_seller_main"/> <!-- Toolbar 可根据需求整合到主内容区域或保留在此 --> <include layout="@layout/tool_bar" android:layout_width="match_parent" android:layout_height="wrap_content"/> </androidx.drawerlayout.widget.DrawerLayout>
关键调整点
- 将
DrawerLayout设为根布局,替代原RelativeLayout - 原Activity内容需包裹在
DrawerLayout的主内容容器内(作为NavigationView的同级元素) - 确保
DrawerLayout只有一个主内容区域,侧滑菜单需通过layout_gravity指定位置
调整后,主内容区域控件不会被覆盖,可正常响应点击,同时Navigation Drawer功能不受影响。
内容的提问来源于stack exchange,提问作者Tasneem
相关产品推荐
相关产品推荐

