android.webkit.WebView是使用WebKit技术的View,主要的用途是显示网页。通过WebView,我们可以在Android应用程序中显示HTML文件或在线网页。


实现方法:

建立一个android工程,编辑.java文件:

package com.android;import android.app.Activity;import android.os.Bundle;import android.webkit.WebView;public class WebTestActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                final String mimetype = "text/html";        final String encoding = "utf-8";                WebView wv;                wv = (WebView) findViewById(R.id.wv);        wv.loadData("<a href ='http://blog.csdn.net/imyang2007?viewmode=contents'>Young's Blog</a>", mimetype, encoding);  }}
用XML layout来对UI布局,编辑main.XML:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <WebView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/wv" />    </LinearLayout>

在这里定义了一个WebView标签,并把该标签的id命名为wv,通过指定ID属性给View的方式,让应用程序在运行时期(run-time)找到相应的view对象。在就是findViewById在代码中的作用,在我们编辑main.XML文件后,R.java文件也会对应更新,我们可以看见在R.java中,多了一个:
    public static final class id {        public static final int wv=0x7f050000;    }

  1. 取得WebVeiw对象后,调用WebView.loadData方法,将HTML内容载入到webview,并显示在Activity上。loadData参数:
    • HTML内容
    • MimeType类型,指定为text/html,即为HTML文件
    • 文字编码方式,utf-8,Unicode方式。
效果图:

点击后:


更多相关文章

  1. Android(安卓)tips2
  2. Android中文API(137) —— LocalSocket
  3. Android之多线程断点下载
  4. Android(安卓)内核编绎错误解决方案
  5. android 自动打包
  6. Android帮助文档(第二部分)开发工具
  7. android 扫描SD卡与系统文件
  8. Android之Adapter用法总结
  9. NPM 和webpack 的基础使用

随机推荐

  1. android sip协议通话实现
  2. Android系统学习总结1--init和Zygote
  3. 8.16 提炼参数对象
  4. 《ionic学习笔记》:设置tabs位于android设
  5. Android(安卓)开发人员必须掌握的 10 个
  6. Android(安卓)Studio使用lambda
  7. Android 多个APK共享数据(Shared User ID
  8. Android获取SDCard路径/Android获取存储
  9. android语音识别方法一:使用intent调用语
  10. android 时间同步