2017-04-21 21 views
0

AdMobにBANNERと添付されましたが、タブレットのサイズが小さいため、サイズをSMART_BANNERに変更しました。AdMob SMART_BANNERの位置

ただし、バナーの位置は下部ではなく、下部から少し上です。

banner position wrong

、これが私のアンドロイド・スタジオのバナーでコードを作成します。

 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

これは私がプログラムでSMART BANNERのadviewをロードする方法である:

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

これは一番下にadviewをロードするための私のコードです:

/** 
* 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); 

    } 
}