NavigationDrawer被SmartGLView遮挡,如何设置使其显示在上层?
这个问题其实是DrawerLayout的子View顺序搞错了,很容易踩坑!
问题根源
DrawerLayout的渲染逻辑是按照XML中子View的顺序来绘制的,后添加的View会绘制在前面的View之上。你现在把NavigationView放在了主内容区域(包含SmartGLView的FrameLayout)的前面,这就导致主内容区域会覆盖在抽屉上面,自然会出现遮挡的情况。
解决方案
只需要调整DrawerLayout的子View顺序:把主内容区域(FrameLayout)放在第一个位置,然后再放NavigationView。这样抽屉展开时就会绘制在主内容(包括SmartGLView)的上层了。
修改后的完整布局代码
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/pl_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <!-- 主内容区域:放在DrawerLayout的第一个子View位置 --> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/pl_tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="line1" android:textAlignment="center" /> <TextView android:id="@+id/pl_tv_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/normal" android:textAlignment="center" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/pl_tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/defaultTime" android:textAlignment="center" /> <TextView android:id="@+id/pl_tv_speed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/defaultSpeed" android:textAlignment="center" /> </LinearLayout> <FrameLayout android:id="@+id/pl_frame_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <fr.arnaudguyon.smartgl.opengl.SmartGLView android:id="@+id/smartGLView" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout> </FrameLayout> <!-- 抽屉NavigationView:放在主内容区域之后 --> <android.support.design.widget.NavigationView android:id="@+id/pl_nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header" app:menu="@menu/drawer_view"> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout>
额外说明
如果调整顺序后还是有问题(比如SmartGLView作为SurfaceView的特殊层级问题),可以尝试给SmartGLView所在的父布局添加android:background属性(比如设为透明色#00000000),或者在代码中动态调整SmartGLView的Z轴顺序:
// 在Activity的onCreate方法中添加 SmartGLView smartGLView = findViewById(R.id.smartGLView); smartGLView.setZOrderOnTop(false); // 确保它不会强制在最上层
不过绝大多数情况下,调整DrawerLayout的子View顺序就能解决问题啦!
内容的提问来源于stack exchange,提问作者hzyu
相关产品推荐
相关产品推荐

