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

横屏时Admob横幅遮挡全屏视频,如何配置垂直线性布局避免视图重叠

问题根因

重叠问题主要来自三个核心错误:

  1. 动态插入视频视图的顺序与权重分配逻辑存在测量时序问题,AdView加载完成后高度变化未触发父布局重新计算权重占比
  2. 固定的Banner尺寸不适配横竖屏切换,横屏时广告尺寸测量错误导致布局偏移
  3. 沉浸式全屏模式下未适配系统栏边距,视图计算可用高度错误
修复步骤

1. 修正全屏布局结构,明确视图层级

直接在布局中预设视频容器,避免动态插入导致的层级混乱,修改layout_fullscreen_video.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:background="@color/black">

    <!-- 视频容器:权重占1,自动占用除广告外的所有剩余空间 -->
    <FrameLayout
        android:id="@+id/video_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <include layout="@layout/layout_ad_banner" />
</LinearLayout>

2. 修改动态视图加载逻辑

将视频视图添加到预设容器中,替换原直接插入根线性布局的逻辑:

// 保留原有逻辑不变,替换下面两行代码
// 原代码:
// decorView.addView(fullscreenContainerView, COVER_SCREEN_PARAMS)
// fullscreenContainerView?.addView(customView, 0, LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0).also { it.weight = 1f })

// 替换为:
decorView.addView(fullscreenContainerView, COVER_SCREEN_PARAMS)
fullscreenContainerView?.findViewById<FrameLayout>(R.id.video_container)
    ?.addView(customView, FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT
    ))

3. 适配横竖屏的广告动态尺寸

删除布局中固定的adSize配置,改为代码动态适配当前屏幕尺寸,先修改layout_ad_banner.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     
android:layout_width="match_parent"     
android:layout_height="wrap_content"     
xmlns:app="http://schemas.android.com/apk/res-auto">   

<com.google.android.gms.ads.AdView         
android:id="@+id/ad_view"         
android:layout_width="match_parent"
android:layout_height="wrap_content"         
app:adUnitId="@string/banner_ad_unit_id" />  
</FrameLayout>

修改AdView初始化逻辑,动态计算适配尺寸并监听广告加载完成事件强制重绘:

fullscreenContainerView?.findViewById<AdView>(R.id.ad_view)?.let { adView ->
    // 动态计算当前屏幕适配的广告尺寸
    val displayMetrics = resources.displayMetrics
    val adWidth = (displayMetrics.widthPixels / displayMetrics.density).toInt()
    adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth))
    AdsManager.get.initAdView(adView)
    // 广告加载完成后强制父布局重排,避免高度变化导致重叠
    adView.adListener = object : AdListener() {
        override fun onAdLoaded() {
            fullscreenContainerView?.requestLayout()
        }
    }
}

4. 配置Activity横竖屏切换不重建

在AndroidManifest.xml对应Activity节点添加configChanges配置,避免切换方向时Activity重建导致布局错乱:

<activity
    android:name=".你的Activity类名"
    android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"
    android:exported="true" />

添加横竖屏切换时的广告刷新逻辑,重写Activity的onConfigurationChanged方法:

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
    fullscreenContainerView?.findViewById<AdView>(R.id.ad_view)?.let { adView ->
        val displayMetrics = resources.displayMetrics
        val adWidth = (displayMetrics.widthPixels / displayMetrics.density).toInt()
        adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth))
        adView.loadAd(AdRequest.Builder().build())
        fullscreenContainerView?.requestLayout()
    }
}
注意事项
  • 确认COVER_SCREEN_PARAMS参数为宽高均匹配父布局,不要设置固定边距
  • 若开启了隐藏导航栏的沉浸式模式,可给广告根布局添加等于导航栏高度的底部边距,避免广告被导航栏遮挡

内容的提问来源于stack exchange,提问作者Dion Assydqi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 09:36:01