android 嵌套webview 全屏展示 页面延伸到状态栏且不被底部导航栏遮挡

发布于:2025-08-29 ⋅ 阅读:(13) ⋅ 点赞:(0)

我的项目是使用webview嵌套了一个uniapp打包出的h5
本来展示很正常,某天突然发现uniapp的底部导航被手机底部的导航栏挡住了,离奇,某天突然出现的
有些手机会设置展示底部导航按钮,有些手机会关闭底部导航
以下代码对这两种情况通用

package org.topsec.viewer

import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import kotlin.concurrent.thread
import android.view.View
// Singleton
class MainActivity : AppCompatActivity() {
    val spiceJNI = SpiceJNI(this)
    val spiceJSInterface = SpiceJSInterface(this)
    val spiceWebViewClient = SpiceWebViewClient(this)
    private lateinit var layoutView : View
    lateinit var webView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContentView(R.layout.webview_layout)
        supportActionBar?.hide()

        webView = findViewById(R.id.web_view)
        val rootView = findViewById<FrameLayout>(R.id.root_frame)

        ViewCompat.setOnApplyWindowInsetsListener(rootView) { view, insets ->
            val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())

            Log.d("InsetsDebug", "Top system bar height: ${systemBarsInsets.top}px")
            Log.d("InsetsDebug", "Bottom system bar height: ${systemBarsInsets.bottom}px")

            // 为根布局设置所有方向的内边距
            view.updatePadding(
                left = systemBarsInsets.left,
                0,
                right = systemBarsInsets.right,
                bottom = systemBarsInsets.bottom
            )

            insets
        }

        // 其他WebView设置
        webView.settings.javaScriptEnabled = true
        webView.webViewClient = spiceWebViewClient
        webView.settings.supportZoom()
        webView.settings.loadWithOverviewMode = true
        webView.settings.builtInZoomControls = true
        webView.settings.displayZoomControls = true
        webView.addJavascriptInterface(spiceJSInterface, "spiceInterface")
        webView.settings.domStorageEnabled = true
        webView.settings.databaseEnabled = true

        webView.loadUrl("file:///android_asset/web/index.html")
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到