将网页嵌入到app中案例实现

来源:互联网 发布:mac spine1.7.03 安装 编辑:程序博客网 时间:2024/05/21 17:07

在这里先说明一下用到的软件有eclipse中的控件有WebView,这个控件像一个网页,是一个WebKit模块Java层的视图类,说的官方了哈,又出来一个新词WebKit,它呢就是一个公开源代码的项目、浏览器内核,像我们平常上网,为什么谷歌,安卓浏览器比那个IE上网快,就是因为他们用的引擎不一样,谷歌呢就是用的WebKit,好了,废话不多说,直接上码!

1.先写一个html网页,在这里省懒劲儿,只是看下效果,直接写个简单的,代码如下:

 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is example!</title>
</head>
<body>

<h3>招聘信息</h3>
   <ul>
     <li>北京</li>
     <li>上海</li>
     <li>广州</li>
     <li>深圳</li>
     <li>邯郸</li>
   </ul>

</body>
</html>

2.把保存的html文件复制到eclipse工程下的assets中(备注:assets文件不会映射R文件,不受影响保留格式)

就是这样的,这里我起的名字为aaa。


3.在eclipse工程下的.xml文件中拖入一个WebView控件,其他的不要,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/wv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

4.让两者合二为一,在MainActivity下敲代码:

public class MainActivity extends Activity {
 WebView wv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        wv1 = (WebView)findViewById(R.id.wv1);
        wv1.loadUrl("file://aaa_asset/aaa.html");
    }


   
}

好了,就到这里了,是不是很简单呢,快去敲敲试试吧!




原创粉丝点击