获得的AdMob和web视图合作 [英] Getting admob and webview working together
本文介绍了获得的AdMob和web视图合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经建立了一个web视图的应用程序工作正常。
我试图加入AdMob的对应用程序的底部,但它不显示。
我已经想通了,如果我设定的主要活动,以
的setContentView(R.layout.activity_main);
我得到的广告,但没有web视图
如果我设置的主要活动,以
的setContentView(mWebview);
我得到的WebView,但没有广告。
我怎样才能既在一起说明了什么?
mainActivity.java
进口android.app.Activity;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.Bundle;
进口android.view.KeyEvent;
进口android.webkit.JavascriptInterface;
进口android.webkit.WebView;
进口android.webkit.WebViewClient;
进口android.widget.Toast;
公共类MainActivity延伸活动{
私人的WebView mWebview;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mWebview =新的WebView(本);
mWebview.getSettings()setJavaScriptEnabled(真)。
最后活动活动=这一点;
mWebview.setWebViewClient(新WebViewClient(){
公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
Toast.makeText(活动介绍Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl(文件:///android_asset/app/index.html);
//的setContentView(mWebview);
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(event.getAction()== KeyEvent.ACTION_DOWN){
开关(钥匙code)
{
案例KeyEvent.KEY code_BACK:
如果(mWebview.canGoBack()==真){
mWebview.goBack();
}其他{
完();
}
返回true;
}
}
返回super.onKeyDown(键code,事件);
}
}
activity_main.xml
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:广告=http://schemas.android.com/apk/lib/com.google.ads
的xmlns:工具=http://schemas.android.com/tools
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>
<的WebView
机器人:ID =@ + ID / webView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
/>
< com.google.ads.AdView
机器人:ID =@ + ID / AD浏览报
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
广告:adSize =大旗
广告:adUnitId =MYIDMYIDMYID
广告:loadAdOnCreate =真
>
< /com.google.ads.AdView>
< / LinearLayout中>
解决方案
请一个指向webView1例如: mWebview =(web视图)findViewById(R.id.webView1);
试试这个:
MainActivity.java
公共类MainActivity延伸活动{
私人的WebView mWebview;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//这将创建一个指向webView1
mWebview =(web视图)findViewById(R.id.webView1);
mWebview.getSettings()setJavaScriptEnabled(真)。
最后活动活动=这一点;
mWebview.setWebViewClient(新WebViewClient(){
公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
Toast.makeText(活动介绍Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl(文件:///android_asset/app/index.html);
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(event.getAction()== KeyEvent.ACTION_DOWN){
开关(钥匙code)
{
案例KeyEvent.KEY code_BACK:
如果(mWebview.canGoBack()==真){
mWebview.goBack();
}其他{
完();
}
返回true;
}
}
返回super.onKeyDown(键code,事件);
}}
main_activity.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:广告=http://schemas.android.com/apk/lib/com.google.ads
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=MainActivity。>
< com.google.ads.AdView
机器人:ID =@ + ID / AD浏览报
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =顶
广告:adSize =大旗
广告:adUnitId =MYIDMYIDMYID
广告:loadAdOnCreate =真正的>
< /com.google.ads.AdView>
<的WebView
机器人:ID =@ + ID / webView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
>
< /的WebView>
I've built a webview app that works fine.
I've tried to add admob to the bottom on app, but it doesnt show.
I've figured out if I set the main activity to
setContentView(R.layout.activity_main);
I get ad, but no webview
If I set the main activity to
setContentView(mWebview );
I get the webview but no ad.
How can I get both to show together?
mainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebview ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("file:///android_asset/app/index.html");
//setContentView(mWebview);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="MYIDMYIDMYID"
ads:loadAdOnCreate="true"
>
</com.google.ads.AdView>
</LinearLayout>
解决方案
Make a pointer to the webView1 ex: mWebview = (WebView)findViewById(R.id.webView1);
try this:
MainActivity.java
public class MainActivity extends Activity {
private WebView mWebview ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this will create a pointer to the webView1
mWebview = (WebView)findViewById(R.id.webView1);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("file:///android_asset/app/index.html");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
ads:adSize="BANNER"
ads:adUnitId="MYIDMYIDMYID"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</WebView>
这篇关于获得的AdMob和web视图合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文