如何在android中使用html作布局文件
时间:2023-01-14 07:49:10
在android开发中,通常使用xml格式来描述布局文件。就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层。大部分企业,其实还是程序员自己动手布局。这样既浪费时间和精力,也未必能达到理想的效果。但是,在企业级的android开发中,使用html页面进行布局,也有很多的优势(例如:简单,大部分开发人员及美工都熟悉,方便统一进行更新,管理)。据笔者了解,已经有不少的公司在使用这种方式进行布局开发。这也可能是一种趋势。
下面,我将给出一个实例代码,供大家学习使用html页面给android应用布局。
package com.dazhuo.ui;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.dazhuo.domain.Person;
import com.dazhuo.service.PersonService;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
public class MainActivity extends Activity {
private PersonService service;
private WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service =new PersonService();
webview = (WebView) this.findViewById(R.id.webView);//android内置浏览器对象
webview.getSettings().setJavaScriptEnabled(true);//启用javascript支持
//添加一个js交互接口,方便html布局文件中的javascript代码能与后台java代码直接交互访问
webview.addJavascriptInterface(new PersonPlugin() , "Person");//new类名,交互访问时使用的别名
// <body onload="javascript:Person.getPersonList()">
webview.loadUrl("file:///android_asset/index.html");//加载本地的html布局文件
//其实可以把这个html布局文件放在公网中,这样方便随时更新维护 例如 webview.loadUrl("www.xxxx.com/index.html");
}
//定义一个内部类,从java后台(可能是从网络,文件或者sqllite数据库) 获取List集合数据,并转换成json字符串,调用前台js代码
private final class PersonPlugin{
public void getPersonList(){
List<Person> list = service.getPersonList();//获得List数据集合
//将List泛型集合的数据转换为JSON数据格式
try {
JSONArray arr =new JSONArray();
for(Person person :list)
{
JSONObject json =new JSONObject();
json.put("id", person.getId());
json.put("name", person.getName());
json.put("mobile",person.getMobile());
arr.put(json);
}
String JSONStr =arr.toString();//转换成json字符串
webview.loadUrl("javascript:show('"+ JSONStr +"')");//执行html布局文件中的javascript函数代码--
Log.i("MainActivity", JSONStr);
} catch (Exception e) {
// TODO: handle exception
}
}
//打电话的方法
public void call(String mobile){
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile));
startActivity(intent);
}
}
}
package com.dazhuo.domain;
public class Person {
private Integer id;
public Integer getId() {
return id;
}
public Person(Integer id, String name, String mobile) {
super();
this.id = id;
this.name = name;
this.mobile = mobile;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
private String name;
private String mobile;
}
package com.dazhuo.service;
import java.util.ArrayList;
import java.util.List;
import com.dazhuo.domain.Person;
public class PersonService {
public List<Person> getPersonList()
{
List<Person> list =new ArrayList<Person>();
list.add(new Person(32, "aa", "13675574545"));
list.add(new Person(32, "bb", "13698874545"));
list.add(new Person(32, "cc", "13644464545"));
list.add(new Person(32, "dd", "13908978877"));
list.add(new Person(32, "ee", "15908989898"));
return list;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function show(jsondata){
var jsonobjs = eval(jsondata);
var table = document.getElementById("personTable");
for(var y=0; y<jsonobjs.length; y++){
var tr = table.insertRow(table.rows.length); //添加一行
//添加三列
var td1 = tr.insertCell(0);
var td2 = tr.insertCell(1);
td2.align = "center";
var td3 = tr.insertCell(2);
td3.align = "center";
//设置列内容和属性
td1.innerHTML = jsonobjs[y].id;
td2.innerHTML = jsonobjs[y].name;
td3.innerHTML = "<a href='javascript:Person.call(\""+ jsonobjs[y].mobile+ "\")'>"+ jsonobjs[y].mobile+ "</a>";
}
}
</script>
</head>
<!-- js代码通过webView调用其插件中的java代码 -->
<body onload="javascript:Person.getPersonList()">
<table border="0" width="100%" id="personTable" cellspacing="0">
<tr>
<td width="20%">编号</td><td width="40%" align="center">姓名</td><td align="center">电话</td>
</tr>
</table>
<a href="javascript:window.location.reload()">刷新</a>
</body>
</html>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中的应用程序接口介绍及实现,密封类与密封方法
VsCode使用EmmyLua插件调试Unity工程Lua代码的详细步骤
![](https://img.aspxhome.com/file/2023/0/81130_0s.png)
java开发RocketMQ之NameServer路由管理源码分析
![](https://img.aspxhome.com/file/2023/6/61266_0s.png)
Java常用函数式接口总结
![](https://img.aspxhome.com/file/2023/8/68178_0s.png)
java @Value(
![](https://img.aspxhome.com/file/2023/7/63017_0s.png)
Java实现图形界面计算器
![](https://img.aspxhome.com/file/2023/7/65757_0s.jpg)
C#获取项目指定目录下文件的方法
Android编程判断网络连接是否可用的方法
Java中保证线程顺序执行的操作代码
快速理解Java垃圾回收和jvm中的stw
C#实现保存文件时重名自动生成新文件的方法
Android使用criteria选择合适的地理位置服务实现方法
![](https://img.aspxhome.com/file/2023/3/138953_0s.png)
Java的MyBatis框架中实现多表连接查询和查询结果分页
![](https://img.aspxhome.com/file/2023/7/72397_0s.png)
利用Jetpack Compose绘制可爱的天气动画
![](https://img.aspxhome.com/file/2023/7/105987_0s.gif)
C#检查foreach判读是否为null的方法
![](https://img.aspxhome.com/file/2023/3/78143_0s.png)
Android调用应用安装界面方法
![](https://img.aspxhome.com/file/2023/4/94954_0s.jpg)
Java微信支付之公众号支付、扫码支付实例
![](https://img.aspxhome.com/file/2023/5/126375_0s.png)
Java异常--常见方法--自定义异常--增强try(try-with-resources)详解
C/C++ Qt TreeWidget 单层树形组件应用小结
![](https://img.aspxhome.com/file/2023/1/112301_0s.png)