Java 如何将Admob与Webview集成

Java 如何将Admob与Webview集成,java,webview,admob,Java,Webview,Admob,我是一名php/web/mysql/css/js开发人员,对java和android都是新手。我的第一个webview很有魅力,但我不明白为什么我的admob根本不起作用。到目前为止,我在这里和www上尝试了几篇教程,但都没有成功。我相信你们中的任何一个极客都会立即发现这个问题,所以请提前感谢。将在下面的主题中添加相关代码,请让我知道我这边有什么问题。 如果你需要更多的资料,我也可以在这里补充 这就是我到目前为止的情况: 主要活动 package smartmedia.de.mywebviewa

我是一名php/web/mysql/css/js开发人员,对java和android都是新手。我的第一个webview很有魅力,但我不明白为什么我的admob根本不起作用。到目前为止,我在这里和www上尝试了几篇教程,但都没有成功。我相信你们中的任何一个极客都会立即发现这个问题,所以请提前感谢。将在下面的主题中添加相关代码,请让我知道我这边有什么问题。 如果你需要更多的资料,我也可以在这里补充

这就是我到目前为止的情况:

主要活动

package smartmedia.de.mywebviewapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import static smartmedia.de.mywebviewapp.AnalyticsWebInterface.TAG;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends Activity {

    private WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        mWebView.addJavascriptInterface(new MyTtsTalker(this), "MyTtsTalker");
        mWebView.addJavascriptInterface(new AnalyticsWebInterface(this), TAG);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setAppCacheEnabled(false);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.loadUrl("file:///android_asset/index.html");

        MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    }

    @Override
    public void onBackPressed()
    {
        if(mWebView.canGoBack())
        {
            mWebView.goBack();
        }
        else
        {
            super.onBackPressed();
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/adView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>


</RelativeLayout>

按如下方式更改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">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_above="@id/adView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        ads:adSize="BANNER"
        ads:adUnitId="@string/footer_ad_id"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"/>
<!--
   In the above adView, change height from wrap_content to some particular 
   size, 30dp, for example to see if you get the desired layout. 
   Even if the ad doesn't load, you know the layout is right because there
   will be a black color where the ad should load.
-->

</RelativeLayout>

您需要更改xml文件,如下所示:

您的adView横幅在页面底部可见,或者整个空间仅由webview占据?或者广告没有加载?请详细说明这一点。我目前正在尝试显示任何广告,我没有花任何时间定位和选择一种类型的广告。它根本没有显示。目前,我更喜欢在我的网络视图上播放一些全屏广告,并从那里开始。Webview占据了整个空间,我不知道如何将广告和Webview对齐?也许它显示在我的网络视图后面?抱歉,我对android/java完全陌生。问题在于您在xml中使用的层次结构,横幅隐藏在您的webview后面,因此不会显示。将parent RelativeLayout更改为LinearLayout,并为其赋予属性“android:orientation:vertical”,对于全屏广告,您需要实施间隙广告。点击此链接以熟悉全屏广告仍然没有-我正在使用“Run\Run Main Activity”进行测试.我刚刚更新了上面的内容,现在屏幕底部至少有一个小的白色区域,高度为50dp,但仍然没有显示任何广告。但这是一个步骤!谢谢你!这会导致底部出现一个小的白色边框,除了白色背景外,什么也看不到。你在为广告使用测试横幅id吗?还没有id,我以前有id代码要测试,但不知道在哪里以及如何添加它。这就是为什么。您可以看到,在没有任何要加载的广告的情况下,高度和宽度为wrap_内容的广告adView的大小为0。尝试为高度指定一些值,比如25dp。@SaschaGrindau我已经相应地更改了答案,请检查并让我知道。->xmlns:tools=”“名称空间声明从未使用过,并且在启动时不会显示任何广告。
<?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">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_above="@id/adView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        ads:adSize="BANNER"
        ads:adUnitId="@string/footer_ad_id"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true"/>
<!--
   In the above adView, change height from wrap_content to some particular 
   size, 30dp, for example to see if you get the desired layout. 
   Even if the ad doesn't load, you know the layout is right because there
   will be a black color where the ad should load.
-->

</RelativeLayout>
You need to change your xml file like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:ads="http://schemas.android.com/apk/res-auto">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"

        />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/footer_ad_id"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
    </com.google.android.gms.ads.AdView>


</LinearLayout>