如何在RelativeLayout中配置WebView与Admob广告避免布局重叠
问题原因
- RelativeLayout为叠加布局,后声明的View默认会绘制在先声明View的上层
- 当前WebView高度设为
match_parent,占满父布局全部高度,固定在底部的AdView直接覆盖在WebView底部内容区域,触发Admob合规警告
修复方案
仅需要在WebView的配置中添加android:layout_above属性,约束WebView底部边界在广告位上方,即可避免内容重叠,修改后的完整布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <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="match_parent" android:layout_height="match_parent" tools:context=".TeamInfo.Team1"> <com.github.ybq.android.spinkit.SpinKitView android:id="@+id/spin_kit" style="@style/SpinKitView.Large.Circle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" app:SpinKit_Color="#E53935" android:layout_centerInParent="true"/> <ImageView android:id="@+id/teamimage1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/commoninfo" android:visibility="gone"/> <!-- 仅修改WebView部分,添加layout_above属性 --> <WebView android:id="@+id/team1web" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/banner_container1" android:visibility="gone"> </WebView> <!--Google Admob Code--> <com.google.android.gms.ads.AdView android:id="@+id/banner_container1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" app:adSize="BANNER" app:adUnitId="@string/ad_id"> </com.google.android.gms.ads.AdView> </RelativeLayout>
注意事项
你贴出的第一版代码存在属性名丢失下划线的语法错误,例如layout width、ad id等,这类错误会导致布局编译失败,建议直接使用上述修正后的代码即可。
内容的提问来源于stack exchange,提问作者Imtiaz Shuvo
相关产品推荐
相关产品推荐

