【问题标题】:Android AdMob Adaptive banner in Fragment on kotlin?kotlin 上 Fragment 中的 Android AdMob 自适应横幅?
【发布时间】:2020-10-13 16:10:11
【问题描述】:

官方文档Adaptive Banner 仅关于活动。如果我尝试将此应用于片段,我会得到Unresolved reference: windowManager。如果我想将WindowManager interface 添加到我的片段中,那么我需要重写 5 个我不知道该怎么做的方法。 addView(), updateViewLayout(), removeView(), getDefaultDisplay(), removeViewImmidiate()。有没有人有一个现成的例子来说明如何将自适应横幅添加到片段?

private val adSize: AdSize
get() {
  val display = windowManager.defaultDisplay
  val outMetrics = DisplayMetrics()
  display.getMetrics(outMetrics)

  val density = outMetrics.density

  var adWidthPixels = ad_view_container.width.toFloat()
  if (adWidthPixels == 0f) {
    adWidthPixels = outMetrics.widthPixels.toFloat()
  }

  val adWidth = (adWidthPixels / density).toInt()
  return AdSize.getCurrentOrientationBannerAdSizeWithWidth(this, adWidth)
}

【问题讨论】:

    标签: android kotlin admob banner


    【解决方案1】:

    我设置了类似于official documentation 的主要活动,但有一些小改动:

    1. adView 变量放在伴随对象中,以便片段可以访问它
    2. 将活动的onCreatead_view_container.addView(adView) 移动到包含ad_view_container 的片段的onViewCreated
    3. 从活动中删除了对广告视图容器宽度 (ad_view_container.width.toFloat()) 的检查,因为 ad_view_container 现在位于片段中。因此,广告必须使用全屏宽度。也许有一些方法可以从片段中获取 ad_view_container 的宽度,但对我来说这不是必需的,因为我想要一个全宽度的广告。
    class MyActivity : AppCompatActivity() {
      
      companion object {
        lateinit var adView: AdView
      }
    
      // Set the ad size to the full screen width.
      private val adSize: AdSize
        get() {
          val display = windowManager.defaultDisplay
          val outMetrics = DisplayMetrics()
          display.getMetrics(outMetrics)
    
          val density = outMetrics.density
    
          val adWidthPixels = outMetrics.widthPixels.toFloat()
    
          val adWidth = (adWidthPixels / density).toInt()
          return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
        }
    
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)
    
        // Initialize the Mobile Ads SDK.
        MobileAds.initialize(this) { }
    
        adView = AdView(this)
        loadBanner()
      }
    
      private fun loadBanner() {
        adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" //TODO - change
        adView.adSize = adSize
    
        // Create an ad request.
        val adRequest = AdRequest.Builder().build()
    
        // Start loading the ad in the background.
        adView.loadAd(adRequest)
      }
    
    }
    

    片段:

    class HomeFragment : Fragment() {
    
        ...
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            // Add the adaptive banner to its container
            ad_view_container.addView(adView)
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
    
            // Remove the adaptive banner from its container
            adViewContainer.removeAllViews()
        }
    
    }
    

    当然还有片段布局xml文件中的ad_view_container

    ...
    
        <FrameLayout
            android:id="@+id/ad_view_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
    
    ...
    

    The result

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多