1

我将 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);

我如何更改代码?谢谢。

4

3 回答 3

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());
于 2017-04-30T04:11:21.550 回答
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" />
于 2017-04-21T14:49:14.543 回答
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);

    }
}
于 2017-08-22T06:23:37.180 回答