如何将Admob与Webview集成
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将Admob与Webview集成相关的知识,希望对你有一定的参考价值。
我是一个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>
的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:3.1.2'
classpath 'com.google.firebase:firebase-ads:15.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
答案
像这样改变你的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>
另一答案
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>
以上是关于如何将Admob与Webview集成的主要内容,如果未能解决你的问题,请参考以下文章