如何在WebView中嵌入本地组件

12 篇文章 0 订阅
1. 首先下载相关文件,一个jar包,一个js文件,下载地址:
http://code.google.com/p/weblayout/downloads/list

2. 新建一个Android工程,导入jar包:weblayout-1.0.java,将weblayout.js拷贝到assets目录下。

3. 修改layout文件layout/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">
<de.enough.weblayout.WebLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/weblayout">
<EditText android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello wrold"/>
</de.enough.weblayout.WebLayout>
</LinearLayout>


4. 新建HTML文件,取名为index.html,放入assets目录下。

<html>
<head>
<script type="text/javascript" src="weblayout.js"></script>
<style type="text/css">
.box {
width: 60%;
margin: auto;
border: 1px solid blue;
}

.header {
background: blue;
font-size: 16pt;
color: white;
}

.content {
padding: 10px;
}

.field {
font-size: 16pt;
color: black;

}
</style>
</head>
<body>
<div class="box">
<div class="header">
Login
</div>
<div class="content">
<div class="field">
Username<br/>
<div id="username"/>
</div>
<div class="field">
Password<br/>
<div id="password"/>
</div>
<div id="login"/>
</div>
</div>
</body>
</html>


注意:一定要记得导入weblayout.js。

5. 修改XXActivity代码。

package com.test;

import de.enough.weblayout.WebLayout;
import android.app.Activity;
import android.os.Bundle;

public class Test_WebLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

WebLayout webLayout = (WebLayout) findViewById(R.id.weblayout);
webLayout.loadUrl("file:///android_asset/index.html");
}
}


最后运行即可,效果如下图,工程源码见附件。
[img]http://dl.iteye.com/upload/attachment/0064/0676/2dc98b07-9388-3db0-8017-98697ed0de8e.bmp[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值