使用WebView加载本地网页
来源:互联网 发布:网购电影票软件 编辑:程序博客网 时间:2024/05/17 05:14
效果图
首先将要加载的网页拷贝到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;@Overrideprotected 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();}@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; default: break; } return super.onOptionsItemSelected(item);}}
到这里就可以愉快的加载本地网页了,有兴趣的同学可以试下哈。
阅读全文
0 0
- 使用WebView加载本地网页
- WebView 加载本地网页
- 使用WebView加载网页
- WebView网页加载使用
- webview加载本地网页乱码问题
- Android webView 加载网页时,使用本地图片替换网页内的图片
- android使用webview加载网页
- android使用webview加载网页
- android:使用webview加载网页
- 使用WebView自定义加载网页
- android使用webview加载网页
- Android使用WebView加载网页
- 使用Webview加载html网页
- Android使用WebView加载网页
- Android使用WebView加载网页
- Android使用WebView加载本地资源
- Android使用WebView加载本地资源
- Android使用WebView加载本地资源
- Struts1和Struts2的区别和对比:
- 前端可视化编程:liveReload安装,sublime 3
- spring中使用字符串注入collection,如set/array
- Error: Spawn SVN ENOENT
- 窗口的布局
- 使用WebView加载本地网页
- MQTT_笔记
- 来说说(Facade)外观模式
- 关于怎么在mysql中执行*.sql文件
- 整理ING
- SonarQube的安装
- fabric下载文件
- Redis:ServiceStack.Redis之IRedisClient
- Cocoa pods的安装和使用详解