【问题标题】:How can I retain a PublisherAdView on an orientation change?如何在方向更改时保留 PublisherAdView?
【发布时间】:2014-06-03 07:40:46
【问题描述】:

我正在使用PublisherAdView 在我的应用中显示横幅广告。当应用程序启动时,我创建 PublisherAdView,将其添加到布局中,然后请求广告。我想在方向更改后显示已请求的相同广告。目前 PublisherAdView 在方向更改时被销毁并重新创建,因此我丢失了所请求的广告。

这是我在片段中创建 PublisherAdView 的方法:

PublisherAdView banner_ad_view = new PublisherAdView(getActivity());
banner_ad_view.setAdUnitId(ad_unit_id);
banner_ad_view.setAdSizes(new AdSize(ad_width, ad_height));

这是我将 PublisherAdView 添加到我的片段布局的方法:

int id = R.id.fragment_banner_ad_container;
RelativeLayout ad_container = (RelativeLayout) fragment_view.findViewById(id);
ad_container.addView(banner_ad_view);

这是我将广告加载到 PublisherAdView 中的方法(使用 PublisherAdRequest):

banner_ad_view.loadAd(ad_request);

我一直在努力避免使用android:configChanges,因为我确实有不同的资源需要根据方向进行管理。

非常感谢任何帮助!

【问题讨论】:

    标签: android android-layout android-fragments ads android-orientation


    【解决方案1】:

    试试这个:

    在你想要的活动中的 android 清单上

    android:configChanges="orientation|screenSize|keyboardHidden" >
    

    在主要活动中:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        }
    }
    

    这对我有用

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 2013-08-13
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      相关资源
      最近更新 更多