需求:我们需要做一个h5页面,并且可以现实加载更多,并且头部我们想要加一个视频播放器,因为h5不够丝滑。
话不多说咱们直接上代码
Xml布局:文章来源:https://www.toymoban.com/news/detail-520583.html
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/vView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
class MainActivity : AppCompatActivity() {
var url1: String =
"https://www.baidu.com/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var mView = findViewById<WebView>(R.id.vView)
var topRelativeLayout = RelativeLayout(this)
val topLp = RelativeLayout.LayoutParams(
ScreenUtils.getScreenWidth(this), 400
)
topRelativeLayout.layoutParams = topLp
val lp = RelativeLayout.LayoutParams(
ScreenUtils.getScreenWidth(this) - 30, 360
)
lp.leftMargin = ScreenUtils.dip2px(this, 15f)
lp.rightMargin = ScreenUtils.dip2px(this, 15f)
lp.topMargin = ScreenUtils.dip2px(this, 20f)
lp.addRule(RelativeLayout.CENTER_HORIZONTAL)
topRelativeLayout.addView(getViewTop(), lp)
mView.addView(topRelativeLayout)
val wSet: WebSettings = mView.settings
wSet.javaScriptEnabled = true
//设置自适应屏幕
wSet.useWideViewPort = true //将图片调整到适合webview的大小
wSet.loadWithOverviewMode = false // 缩放至屏幕的大小
mView.loadUrl(url1)
mView.webViewClient = object : WebViewClient() {
// 链接跳转都会走这个方法
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
Log.d("MainActivity", "Url:$url")
view.loadUrl(url) // 强制在当前 WebView 中加载 url
return true
}
}
}
private fun getViewTop(): View? {
return LayoutInflater.from(this).inflate(R.layout.view_top, null)
}
}
文章来源地址https://www.toymoban.com/news/detail-520583.html
到了这里,关于WebView头部添加android原生视频播放的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!