横幅 Banner 广告样式

优质
小牛编辑
161浏览
2023-12-01

横幅(Banner)

1. 确定SDK文件已经全部拷贝到工程文件的libs子目录下参考

2. 在需要添加Banner广告的Activity对应的布局文件中加入一个AdView[com.hubcloud.adhubsdk.AdView]控件来给样式定位

  • 例如:

      <!-- view for AdHub Banner Ad -->
      <com.hubcloud.adhubsdk.AdView
          android:id="@+id/adView" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          ads:adUnitId="@string/banner_ad_unit_id" />
    

注意:AdView的宽和高建议分别写为match_parentwrap_content,须要添加adView.setResizeAdToFitContainer(true)

3. 添加广告代码,添加广告代码前请确定已初始化SDK

  • 例如:
    private AdView adView;

    adView = (AdView) findViewById(R.id.adView);
    adView.setAdUnitId(adUnitId);
    adView.setResizeAdToFitContainer(true);
    adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                Log.i("lance", "onAdLoaded");
            }

            @Override
            public void onAdShown() {
                Log.i("lance", "onAdShown");
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Log.i("lance", "onAdFailedToLoad");
            }

            @Override
            public void onAdLeftApplication() {
                Log.i("lance", "onAdLeftApplication");
            }

            @Override
            public void onAdClosed() {
            Log.i("lance", "onAdClosed");
            }

            @Override
            public void onAdOpened() {
                Log.i("lance", "onAdOpened");
            }

            @Override
            public void onAdClicked() {
                Log.i("lance", "onAdClicked");
            }
        });
    final AdRequest adRequest = new AdRequest.Builder().build();
    //建议使用此方法调用loadAd方法
    adView.post(new Runnable() {
            @Override
            public void run() {
                adView.loadAd(adRequest);
            }
        });

    //banner会定时刷新,所以当Fragment不可见或者Activity消失的时候请取消
    @Override
    protected void onDestroy() {
        adView.cancel();
        super.onDestroy();
    }

PS:更多设置请参考Demo中的com.hubcloud.adhubsdkdemo.BannerActivity的代码.