美文网首页
Android使用WebView加载html地址或文件

Android使用WebView加载html地址或文件

作者: 浅_若清风 | 来源:发表于2021-06-18 18:20 被阅读0次

首先在布局xml里面指定WebView根节点

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/web_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

在.java的onCreate()里使用

private WebView webview;
private String filePath;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    webview = findViewById(R.id.web_content);
    initWebView();
    //1. asset目录下的index.html文件
    filePath = "file:///android_asset/index.html";
    //2.本地sd卡内的index.html文件 
    filePath = "content://com.android.htmlfileprovider/sdcard/index.html";
    //3.指定的URL的html文件 
    filePath = "http://wap.baidu.com";
   //加载html
   if (filePath != null) {
    webview.loadUrl(filePath); 
   }
}
private void initWebView()
{
WebSettings settings = mContentWv.getSettings();
        //设置支持缩放
        settings.setBuiltInZoomControls(true); 
       // 缩放至屏幕的大小
        settings.setLoadWithOverviewMode(true); 
        settings.setJavaScriptEnabled(true);
}

相关文章

网友评论

      本文标题:Android使用WebView加载html地址或文件

      本文链接:https://www.haomeiwen.com/subject/nwhlyltx.html