Android Studio:Webview上的横幅广告

Android Studio:Webview上的横幅广告,android,webview,admob,Android,Webview,Admob,如何在webview上添加谷歌广告横幅? 它应该显示在底部和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/

如何在webview上添加谷歌广告横幅? 它应该显示在底部和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>


我相信你可以保持年轻,但我喜欢以下方式。希望这有帮助:)

我尝试了你的解决方案,但广告仍然没有出现。广告根本没有出现?这可能是adview本身存在的一个问题等等,您是否复制并粘贴了和IDE中完全相同的代码?因为如果是这样的话,在您的代码中,您没有关闭WebView标记,广告工作正常。当我删除webview时,广告就会出现。但是用webview你看不到它。我已经关闭了代码中的标签,这只是一个错误。现在它终于起作用了,我真的不知道是什么错误。当我在PC上运行应用程序时,它会显示广告。但当我构建apk并在手机上尝试时,广告不会显示。