android用WebView显示包含文字和图片的本地网页(支持放大缩小)

前言

在android的实际开发中有时候我们需要将文字和图片一起显示,可以用TextView和ImageView的组合来实现,其实用网页显示更容易,本文介绍android显示含有图片和文字的本地网页html,hml的方法,并支持放大缩小。

注:本文demo源码下载地址:http://download.csdn.net/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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值