前言
在android的实际开发中有时候我们需要将文字和图片一起显示,可以用TextView和ImageView的组合来实现,其实用网页显示更容易,本文介绍android显示含有图片和文字的本地网页html,hml的方法,并支持放大缩小。
注:本文demo源码下载地址:http://download.youkuaiyun.com/detail/dxzysk/5788093
一、效果图
Demo制作的网页显示效果如下图所示:
界面不是很漂亮,大家按照需要修改一下
二、部分代码
package nishik.ncc.html;
import java.io.InputStream;
import nishik.ncc.html.R;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class ViewHtml extends Activity
{
WebView wvShow;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序中的WebView组件
wvShow = (WebView) findViewById(R.id.show);
try {
// wvShow.loadDataWithBaseURL(null, readText()
// , "text/html" , "utf-8", null);
//wvShow.loadUrl("/assets/help/main1.htm");
wvShow.loadUrl("file:///android_asset/help/2008.htm");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wvShow.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
wvShow.getSettings().setSupportZoom(true);
wvShow.getSettings().setBuiltInZoomControls(true);
}
public String readText() throws Exception
{
//InputStream is = this.getClass()
// .getResourceAsStream("/assets/text.txt");
//InputStream is = getAssets().open("main1.htm");
//InputStream is = getAssets().open("main1001.htm");
InputStream is = this.getClass()
.getResourceAsStream("/assets/help/main1.htm");
int index = is.available();
byte data[] = new byte[index];
is.read(data);
return new String(data, "UTF-8");
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>