我将AdMob附加为BANNER
,但是平板电脑的尺寸很小,所以我把它的尺寸更改为SMART_BANNER
。
但是横幅的位置不在底部,而是在底部以上。
这是我的android-studio横幅创建代码。
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 :(得分: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" />
答案 1 :(得分:0)
这是我以编程方式加载SMART BANNER广告的方式:
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());
答案 2 :(得分:0)
这是我在底部加载广告的代码:
/**
* 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);
}
}