使用WebView加载本地网页
2017-06-27 14:29
363 查看
效果图
首先将要加载的网页拷贝到assets目录下
-布局代码就一个WebView
MainActivity代码
到这里就可以愉快的加载本地网页了,有兴趣的同学可以试下哈。
首先将要加载的网页拷贝到assets目录下
-布局代码就一个WebView
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" />
MainActivity代码
public class MainActivity extends AppCompatActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar supportActionBar = getSupportActionBar(); if (supportActionBar != null) { supportActionBar.setDisplayHomeAsUpEnabled(true); } mWebView = (WebView) findViewById(R.id.webView); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDefaultTextEncodingName("UTF-8"); //防止WebView滚动时背景变成黑色 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { mWebView.setBackgroundColor(0x00000000); } else { mWebView.setBackgroundColor(Color.argb(1, 0, 0, 0)); } try { mWebView.loadDataWithBaseURL(null, readAssetsFile(getAssets().open("test.html")), "text/html", "utf-8", ""); } catch (Exception e) { Toast.makeText(this, "加载错误", Toast.LENGTH_SHORT).show(); } } /** * 从输入流返回字符串 * @param inputStream * @return */ private String readAssetsFile(InputStream inputStream) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len; try { while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } inputStream.close(); outputStream.close(); } catch (Exception e) { } return outputStream.toString(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; default: break; } return super.onOptionsItemSelected(item); } }
到这里就可以愉快的加载本地网页了,有兴趣的同学可以试下哈。
相关文章推荐
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android 4.0 WebView 使用loadData加载网页乱码之解决办法
- android使用webview加载网页
- android 使用webview加载网页问题
- WebView的返回、前进、刷新加载本地网页等操作
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- 使用WebView加载本地html页面,实现与java之间的相互响应
- 使用WebView, WebChromeClient和WebViewClient加载网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- [Android] 使用 WebView 加载网页而不调用外部浏览器
- WebView加载html实现网页上传本地文件(图片,拍照,语音等)
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- 使用WebView, WebChromeClient和WebViewClient加载网页
- 使用WebView监控网页加载状况,PerformanceMonitor,WebViewClient生命周期
- Android使用WebView加载本地资源
- Android使用WebView加载本地资源
- Android WebView的使用(禁止超链接调用其他浏览器 设置滚动条 禁止横竖屏切换重新加载网页 )