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

如何让不同屏幕尺寸下WebView与AdMob Banner不重叠?

解决WebView与AdMob Banner在不同屏幕尺寸下的重叠问题

你的问题根源很明确:WebView使用了固定的像素高度(1400px),这是绝对单位,在不同分辨率、屏幕密度的设备上显示效果差异极大。当设备屏幕总高度小于1400px时,WebView底部就会超出屏幕范围,和底部固定的AdView重叠。

下面是修改后的XML布局代码,完全适配多屏幕尺寸:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.median1.psalmsmarket.Psalms">

    <!-- WebView 自适应剩余空间,不会覆盖底部广告 -->
    <WebView 
        android:id="@+id/Pslamsview" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:layout_above="@id/adView" />

    <!-- AdView 固定在底部,WebView会自动避开它 -->
    <com.google.android.gms.ads.AdView 
        android:id="@+id/adView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" 
        ads:adSize="BANNER" 
        ads:adUnitId="@string/banner_ad_unit_id" />

</RelativeLayout>

关键修改点说明:

  • 移除了WebView的固定1400px高度,改用match_parent,同时添加android:layout_above="@id/adView"约束,让WebView自动填充除了AdView之外的所有剩余空间,从屏幕顶部延伸到AdView的顶部。
  • 移除了AdView中多余的tools:layout_editor_absoluteX/Y属性,这些是布局编辑器的辅助属性,运行时无效,反而可能干扰布局逻辑。

另外补充一个小建议:如果希望广告更适配不同屏幕宽度,可以考虑将ads:adSize改为SMART_BANNER,它会根据屏幕宽度自动调整广告尺寸,进一步提升适配性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:59:32