要解决这个问题,可以尝试以下步骤:
具体实现示例代码如下:
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.google.accompanist.webview.WebView
import com.google.accompanist.webview.WebViewClient
@Composable
fun CustomWebView(url: String) {
var loading by remember { mutableStateOf(true) }
Surface(modifier = Modifier.fillMaxSize()) {
Column(modifier = Modifier.fillMaxSize()) {
if (loading) {
// Show a progress indicator when the WebView is loading
// 在WebView加载时显示进度指示器
LinearProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.height(2.dp)
)
}
WebView(
modifier = Modifier.weight(1f),
url = url,
webViewClient = object : WebViewClient() {
override fun onPageStarted(view: android.webkit.WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
loading = true
}
override fun onPageFinished(view: android.webkit.WebView?, url: String?) {
super.onPageFinished(view, url)
loading = false
}
},
onLongPress = { /* Handle onLongPress events */ }
)
}
}
}