首页 > 解决方案 > 横向时 Admob 横幅部分覆盖​​全屏视频

问题描述

如何在我的 kotlin 代码中定义垂直线性布局,以便视图不会在横幅和其他内容之间重叠?

问题是admob广告在横向时会覆盖视频播放器内容

这在我的程序中

    override fun onShowCustomView(view: View, callback: CustomViewCallback, requestedOrientation: Int) {
        val currentTab = tabsManager.currentTab
        if (customView != null) {
            try {
                callback.onCustomViewHidden()
            } catch (e: Exception) {
                logger.log(TAG, "Error hiding custom view", e)
            }

            return
        }

        try {
            view.keepScreenOn = true
        } catch (e: SecurityException) {
            logger.log(TAG, "WebView is not allowed to keep the screen on")
        }

        originalOrientation = getRequestedOrientation()
        customViewCallback = callback
        customView = view

        //setRequestedOrientation(requestedOrientation)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR)
        val decorView = window.decorView as FrameLayout
        
        fullscreenContainerView = LayoutInflater.from(this).inflate(R.layout.layout_fullscrren_video, null) as LinearLayout
        fullscreenContainerView?.setBackgroundColor(ContextCompat.getColor(this, R.color.black))
        if (view is FrameLayout) {
            val child = view.focusedChild
            if (child is VideoView) {
                videoView = child
                child.setOnErrorListener(VideoCompletionListener())
                child.setOnCompletionListener(VideoCompletionListener())
            }
        } else if (view is VideoView) {
            videoView = view
            view.setOnErrorListener(VideoCompletionListener())
            view.setOnCompletionListener(VideoCompletionListener())
        }
        
        decorView.addView(fullscreenContainerView, COVER_SCREEN_PARAMS)
        fullscreenContainerView?.addView(customView, 0, LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0).also { it.weight = 1f })
        fullscreenContainerView?.findViewById<AdView>(R.id.ad_view)?.let { AdsManager.get.initAdView(it) }
        decorView.requestLayout()
        setFullscreen(enabled = true, immersive = true)
        currentTab?.setVisibility(INVISIBLE)
    }

myxml代码

layout_fullscreen_video.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    include layout="@layout/layout_ad_banner" 
</LinearLayout>

这是我的 layout_ad_banner

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     
android:layout_width="match_parent"     
android:layout_height="wrap_content"     
xmlns:app="http://schemas.android.com/apk/res-auto">   

<com.google.android.gms.ads.AdView         
android:id="@+id/ad_view"         
android:layout_width="match_parent"
android:layout_height="wrap_content"         
app:adSize="BANNER"         
app:adUnitId="@string/banner_ad_unit_id" />  
</FrameLayout>

标签: androidkotlinadmob

解决方案


不要在横向模式下加载广告横幅,并在横向模式下单独布局没有广告横幅。


推荐阅读