在屏幕底部显示AdMob横幅

时间:2015-10-05 15:42:23

标签: android admob android-relativelayout

我的AdMob横幅显示在屏幕顶部,而不是显示在底部。

这是我的代码:

adView = new AdView(mSingleton);
adView.setAdSize(AdSize.SMART_BANNER); 
adView.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxx");

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT,
       RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);

AdRequest adRequest = new AdRequest.Builder()
  .addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
  .build();
adView.loadAd(adRequest);
mLayout.addView(adView, params);

感谢任何帮助。 感谢。

3 个答案:

答案 0 :(得分:2)

我明白了!

更改包含adView的布局的重力:

((layoutType) layout).setGravity(Gravity.BOTTOM);

答案 1 :(得分:1)

在你的xml中试试这个。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView 
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <com.google.android.gms.ads.AdView
        android:layout_alignParentBottom="true"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="put ad id here">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

替换AdView的java代码。

 adView = new AdView(mSingleton);
  adView.setAdSize(AdSize.SMART_BANNER); 
  adView.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxx");


  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT,
       RelativeLayout.LayoutParams.WRAP_CONTENT);

  params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  params.addRule(RelativeLayout.CENTER_HORIZONTAL);

  AdRequest adRequest = new AdRequest.Builder()
  .addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
  .build();
  adView.loadAd(adRequest);
  mLayout.addView(adView, params);

有了这个

AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

xml中adView中的行android:layout_alignParentBottom="true"非常重要。放置并尝试。

主要来自https://developers.google.com/admob/android/quick-start

答案 2 :(得分:1)

使用此

重新审核您的广告代码
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="bottom"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx">

</com.google.android.gms.ads.AdView>