gpt4 book ai didi

java - 如何将 Admob 与 Webview 集成

转载 作者:行者123 更新时间:2023-11-30 06:01:34 25 4
gpt4 key购买 nike

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

这就是我到目前为止所拥有的:

主要 Activity

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>

构建.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>

关于java - 如何将 Admob 与 Webview 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com