Android 将admob广告放置在webview的引导窗口
我想要我的应用程序的一些帮助 我使用webview(一个xml文件中只有一个webview)并在其中使用本地html页面 我使用以下java代码放置了admob横幅广告Android 将admob广告放置在webview的引导窗口,android,webview,admob,Android,Webview,Admob,我想要我的应用程序的一些帮助 我使用webview(一个xml文件中只有一个webview)并在其中使用本地html页面 我使用以下java代码放置了admob横幅广告 private AdView adView; adView = new AdView(this, AdSize.BANNER, "my publisher id"); WebView layout = (WebView) findViewById(R.id.webView1);
private AdView adView;
adView = new AdView(this, AdSize.BANNER, "my publisher id");
WebView layout = (WebView) findViewById(R.id.webView1);
layout.addView(adView);
adView.loadAd(new AdRequest());
我的问题是,在启动应用程序时,广告显示在我的应用程序顶部,从而隐藏了我页面上的一些内容,当用户向下滚动时,他无法看到广告。
我怎样才能让广告显示在底部,让用户始终可以看到它
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
由于WebView是绝对布局,因此可以按如下方式定位其子视图:
AbsoluteLayout.LayoutParams params = AbsoluteLayout.LayoutParams(width,height,X-position,Y-position);
adView.setLayoutParams(params);
但是,将WebView置于线性布局会更容易,因此当您添加广告时,它将自动显示在底部。例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
另一种选择是将广告包含在xml中。您可以在AdMob中找到如何执行此操作。摘自现场:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- WebView goes here -->
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</LinearLayout>
以下是最新的布局XML,它支持网页底部的webview和横幅广告。以下是适用于我的代码:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/adView"
android:layout_alignParentTop="true"
android:orientation="vertical">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
这是我发起广告请求的方式:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
我尝试了第二种方法,但logcat中的错误是“没有足够的空间显示广告需求(320,50)已经(320,0)如果您想知道,错误在于所涉及的布局参数的交互导致AdView的高度为0。您必须使用AdView上的适当参数调用setLayoutParams才能使其工作。AdMob已-未-弃用。
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/adView"
android:layout_alignParentTop="true"
android:orientation="vertical">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
dependencies {
compile 'com.google.android.gms:play-services-ads:7.8.0'
}
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);