Android Studio:Webview 上的横幅广告

Android Studio: Banner ad over Webview

如何通过网络视图添加 Google 广告横幅? 它应该显示在底部和 webview 之上而不是之下。

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="89dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="1dp"></com.google.android.gms.ads.AdView>


    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="409dp"

    </WebView>
</androidx.constraintlayout.widget.ConstraintLayout>

您可以将 ConstraintLayout 更改为 RelativeLayout,然后将 webview 放在 AdView 之上。

<?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"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/myWebView"
        android:layout_above="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:layout_width="match_parent"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

我确定您可以保留约束布局,但我喜欢按以下方式进行。希望这会有所帮助:)