2017-04-21 74 views
0

我將AdMob附加爲BANNER,但它的平板尺寸很小,所以我將其尺寸更改爲SMART_BANNERAdMob SMART_BANNER職位

但橫幅的位置不在底部,但從底部稍高。

banner position wrong

,這是我的Android工作室旗幟創建代碼。

 mAdView = new AdView(_appActivity); 
     mAdView.setAdSize(AdSize.SMART_BANNER); 
     mAdView.setAdUnitId(_appActivity.getResources().getString(R.string.ad_banner)); 

     mAdView.loadAd(adRequest); 
     /* 
     LinearLayout linearLayout = new LinearLayout(_appActivity); 

     LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 
     adParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; 
     linearLayout.addView(mAdView, adParams); 
     _appActivity.mFrameLayout.addView(linearLayout); 
     */ 
     RelativeLayout relativeLayout = new RelativeLayout(_appActivity); 
     relativeLayout.setY(0); 
     _appActivity.mFrameLayout.addView(relativeLayout); 


     RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
       AdView.LayoutParams.WRAP_CONTENT, 
       AdView.LayoutParams.WRAP_CONTENT); 
     // align bottom 
     adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
     // align center 
     adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); 


     relativeLayout.addView(mAdView, adViewParams); 

     mAdView.setBackgroundColor(Color.BLACK); 

     mAdView.setBackgroundColor(0); 

如何更改代碼?謝謝。

回答

0

您需要將其底部約束設置爲父項的底部。我不知道如何以編程方式執行此操作,但可以使用XML來實現此目的。例如:

<com.google.android.gms.ads.AdView 
    android:id="@+id/adView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:adSize="SMART_BANNER" 
    app:adUnitId="YourADUnitID" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" /> 
0

我這是怎麼加載程序的智能橫幅廣告查看:

bannerAdView = new AdView(this); 
bannerAdView.setAdUnitId("ca-app-pub-xxx/xxxx"); 
bannerAdView.setAdSize(AdSize.SMART_BANNER); 
bannerAdView.setBackgroundColor(Color.TRANSPARENT); 

RelativeLayout relativeLayout = new RelativeLayout(this); 
mFrameLayout.addView(relativeLayout); 

RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(AdView.LayoutParams.WRAP_CONTENT, AdView.LayoutParams.WRAP_CONTENT); 
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); 
relativeLayout.addView(bannerAdView, adViewParams); 
bannerAdView.loadAd(new AdRequest.Builder().build()); 
0

這是我的代碼在底部加載AD瀏覽:

/** 
* initialiseBannnerAdMob 
* @param view 
*/ 
private void initialiseBannnerAdMob(View view) { 

    public AdView bannerAdview; 

    LinearLayout bannerLayout = (LinearLayout) activity 
      .findViewById(R.id.bannerView); 


    float height = activity.getResources().getDimension(R.dimen.ad_size); 

    float density = activity.getResources().getDisplayMetrics().density; 

    float value = getScreenHeight()/density; 
    if (value <= 400) { 
     if (getPref().getGameMode() == 1) { 
      height = 50 * density; 
     } else { 
      height = 32 * density; 
     } 

     // Toast.makeText(getBaseContext(), "Group 1", 1000).show(); 

    } else if (value > 400 && value <= 720) { 

     height = 50 * density; 
     // Toast.makeText(getBaseContext(), "Group 2", 1000).show(); 

    } else if (value > 720) { 
     height = 90 * density; 
     // /Toast.makeText(getBaseContext(), "Group 3", 1000).show(); 
    } 
    if (bannerLayout != null) { 

     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) bannerLayout 
       .getLayoutParams(); 

     System.out.println("params.height" + height); 

     params.height = (int) height; 
     params.bottomMargin = 0; 
     bannerLayout.setLayoutParams(params); 
    } 
    System.out.println("onad initialiseBannnerAdMob"); 

    if (bannerAdview != null) { 
     bannerAdview.destroy(); 
     bannerAdview = null; 
    } 
    bannerAdview = new AdView(this); 
    bannerAdview.setAdSize(AdSize.SMART_BANNER); 
    bannerAdview.setAdUnitId(MyConstants.AD_FOOTER_BANNER_UNIT_ID); 

    bannerLayout.addView(bannerAdview); 

    bannerAdview.setAdListener(new AdListener() { 

     @Override 
     public void onAdClosed() { 
      // TODO Auto-generated method stub 
      super.onAdClosed(); 
     } 

     @Override 
     public void onAdFailedToLoad(int errorCode) { 

      System.out.println("onAdFailedToLoad banner " + errorCode); 

      // TODO Auto-generated method stub 
      super.onAdFailedToLoad(errorCode); 
     } 

     @Override 
     public void onAdLeftApplication() { 
      // TODO Auto-generated method stub 

      System.out.println("onAdLeftApplication banner "); 

      super.onAdLeftApplication(); 
     } 

     @Override 
     public void onAdLoaded() { 
      // Toast.makeText(activity, "onAdLoaded", 100).show(); 
      System.out.println("onAdLoaded banner "); 

      MyConstants.isShowingBannerView = true; 
      super.onAdLoaded(); 
     } 

     @Override 
     public void onAdOpened() { 
      // TODO Auto-generated method stub 
      super.onAdOpened(); 
     } 

    }); 

    loadBannerAdRequest(); 
} 

/** 
* loadBannerAdRequest 
*/ 
void loadBannerAdRequest() { 

    if (bannerAdview != null) { 

     AdRequest adRequest = new AdRequest.Builder().build(); 
     bannerAdview.loadAd(adRequest); 

    } 
}