You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在MainActivity.xml添加AdMob横幅广告后导航菜单失效、内容被遮挡如何解决

问题原因

  • DrawerLayout的子View绘制优先级按代码声明顺序从低到高,你在根节点最后声明了全屏的RelativeLayout,它的层级高于前面的主内容布局和NavigationView,直接拦截了所有触摸事件,导致导航菜单无法响应滑动、点击操作。
  • 原主内容布局没有预留广告位,广告置于屏幕最底部时会遮挡底层的内容区域。

修复方案

调整布局结构,将广告控件移入主内容的垂直LinearLayout中,同时给内容展示区域设置权重预留广告位,修正后的完整代码如下:

<?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">
    <!-- 主内容垂直布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.governmentjobonline.MainActivity">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_layout" />
        <include layout="@layout/tab_layout" />

        <!-- 内容展示区域,使用权重占满剩余空间 -->
        <FrameLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <!-- 广告位直接放在主布局底部,不需要额外全屏RelativeLayout -->
        <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            ads:adSize="FULL_BANNER"
            ads:adUnitId="@string/admob_banner_id">
        </com.google.android.gms.ads.AdView>
    </LinearLayout>

    <!-- 导航抽屉放在主内容之后 -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:itemTextAppearance="?android:attr/textAppearanceMedium"
        app:menu="@menu/navigation_drawer">
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

额外说明

你原来的主内容LinearLayout里只放了toolbar和tab,没有内容承载容器,后续如果要加载Fragment可以直接放到id为content_container的FrameLayout中即可。

内容的提问来源于stack exchange,提问作者Ram Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 19:51:00