在onResume之前,Admob视图不可见

时间:2014-03-07 09:41:36

标签: java android view admob

AdMob视图不可见,但可以点击。如果调用onResume()视图变得可见。我用的时候 AdMob库工作正常,但如果使用AdMob的google play服务,我就遇到了这个问题。 我的代码如下。

   protected void initAdMob(){
      if (mAdView == null) {
         mAdView = new AdView(MainActivity.this);
         mAdView.setAdUnitId(getString(R.string.admob_id));
         mAdView.setAdSize(AdSize.SMART_BANNER);
         mAdView.loadAd(new AdRequest.Builder().build());
         mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
              super.onAdClosed();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
              super.onAdFailedToLoad(errorCode);
            }

            @Override
            public void onAdLeftApplication() {
              super.onAdLeftApplication();
            }

            @Override
            public void onAdOpened() {
              super.onAdOpened();
            }

            @Override
            public void onAdLoaded() {
              super.onAdLoaded();
            }
         });
       }
    }

    private void showAdMob() {
      initAdMob();
     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(                                                                                     FrameLayout.LayoutParams.MATCH_PARENT,                                         FrameLayout.LayoutParams.WRAP_CONTENT);
     params.gravity = Gravity.CENTER_HORIZONTAL;
     mAdView.setLayoutParams(params);

     FrameLayout fl = (FrameLayout) mainView.findViewById(R.id.adView);
     if (fl != null) {
        fl.removeAllViews();
        fl.addView(mAdView, params);
     }
   }



   @Override
   public void onResume() {
     if(mAdView != null){
       mAdView.resume();      
        }
     super.onResume();
   }

查看xml:

    

    <FrameLayout
            android:id="@+id/gameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <FrameLayout
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

需要在AdMob中使用中介ID而非发布商ID