安卓开发如何正确将广告放置在WebView底部?
问题根因
- 你当前的WebView设置了
android:layout_height="fill_parent",会占满LinearLayout内Toolbar之外的所有空间,AdView被直接挤出屏幕可视区域,所以无法显示。 - AdView添加的
android:layout_alignParentBottom、android:layout_centerHorizontal是RelativeLayout专属属性,在LinearLayout中完全不生效,无法起到固定底部的作用。
广告无法正常渲染展示,就是Facebook Audience平台抛出放置错误警告的原因,修复布局后警告会自动消失。
修复方案1:调整现有LinearLayout配置(改动最小)
仅需修改WebView和AdView的几个属性即可实现效果:
- 将WebView的高度改为
0dp,添加android:layout_weight="1"属性,让它自动占满Toolbar和AdView之外的剩余空间 - 删掉AdView中无效的RelativeLayout属性,添加
android:layout_gravity="center_horizontal"实现广告居中
修改后的完整布局代码如下:
<androidx.constraintlayout.widget.ConstraintLayout 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=".Policy.PrivacyPolicy"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="#E53935" android:textAlignment="center" app:popupTheme="@style/Theme.BangabandhuT20Cup.PopupOverlay" app:title="@string/privacy_policy_title" app:titleTextColor="#FFFFFF"> </androidx.appcompat.widget.Toolbar> <WebView android:id="@+id/policyWebView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </WebView> <!--Google Admob Code--> <com.google.android.gms.ads.AdView android:id="@+id/banner_container00" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" app:adSize="BANNER" app:adUnitId="@string/ad_id"> </com.google.android.gms.ads.AdView> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
修复方案2:直接使用外层ConstraintLayout(更推荐)
删掉冗余的LinearLayout嵌套,直接用ConstraintLayout做约束,布局更扁平、渲染性能更好:
<androidx.constraintlayout.widget.ConstraintLayout 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=".Policy.PrivacyPolicy"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="#E53935" android:textAlignment="center" app:layout_constraintTop_toTopOf="parent" app:popupTheme="@style/Theme.BangabandhuT20Cup.PopupOverlay" app:title="@string/privacy_policy_title" app:titleTextColor="#FFFFFF"> </androidx.appcompat.widget.Toolbar> <WebView android:id="@+id/policyWebView" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintBottom_toTopOf="@id/banner_container00" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> </WebView> <!--Google Admob Code--> <com.google.android.gms.ads.AdView android:id="@+id/banner_container00" android:layout_width="wrap_content" android:layout_height="wrap_content" app:adSize="BANNER" app:adUnitId="@string/ad_id" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> </com.google.android.gms.ads.AdView> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者Imtiaz Shuvo
相关产品推荐
相关产品推荐

