2

我想为我的应用程序提供一些帮助我正在使用 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);
       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

    setContentView(R.layout.webview);
4

2 回答 2

5

因为 WebView 是一个 AbsoluteLayout,所以您可以像这样定位它的子视图:

    AbsoluteLayout.LayoutParams params = AbsoluteLayout.LayoutParams(width,height,X-position,Y-position);
    adView.setLayoutParams(params);

但是,将 WebView 放在 LinearLayout 中会更容易,因此当您添加广告时,它会自动出现在底部。例如:

    <?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>

您的代码必须更改为如下所示:

    private AdView adView;
    adView = new AdView(this, AdSize.BANNER, "my publisher id");
    LinearLayout root = (LinearLayout)findViewById(R.id.main);
    root.addView(adView);
    adView.loadAd(new AdRequest());

另一种选择是在 xml 中包含广告。您可以在 AdMob https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#android开发者指南中了解如何执行此操作 。取自网站:

    <?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>
于 2013-10-02T09:55:56.227 回答
1

这是支持页面底部的 webview 和横幅广告的最新布局 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"
    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>

适当改变@string/banner_ad_unit_id

webView1是 webview 的 id。

如果您的布局抛出 AdView not found 异常,请确保添加play-services-ads依赖项。您必须在应用程序的build.gradle文件中添加/更新它,如下所示:

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);
于 2015-08-22T10:03:04.940 回答