在android开发中,通常使用xml格式来描述布局文件。就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层。大部分企业,其实还是程序员自己动手布局。这样既浪费时间和精力,也未必能达到理想的效果。但是,在企业级的android开发中,使用html页面进行布局,也有很多的优势(例如:简单,大部分开发人员及美工都熟悉,方便统一进行更新,管理)。据笔者了解,已经有不少的公司在使用这种方式进行布局开发。这也可能是一种趋势。

下面,我将给出一个实例代码,供大家学习使用html页面给android应用布局。

  1. package com.dazhuo.ui;
  2. import java.util.List;
  3. import org.json.JSONArray;
  4. import org.json.JSONObject;
  5. import com.dazhuo.domain.Person;
  6. import com.dazhuo.service.PersonService;
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.webkit.WebView;
  13. public class MainActivity extends Activity {
  14. private PersonService service;
  15. private WebView webview;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. service =new PersonService();
  21. webview = (WebView) this.findViewById(R.id.webView);//android内置浏览器对象
  22. webview.getSettings().setJavaScriptEnabled(true);//启用javascript支持
  23. //添加一个js交互接口,方便html布局文件中的javascript代码能与后台java代码直接交互访问
  24. webview.addJavascriptInterface(new PersonPlugin() , "Person");//new类名,交互访问时使用的别名
  25. // <body onload="javascript:Person.getPersonList()">
  26. webview.loadUrl("file:///android_asset/index.html");//加载本地的html布局文件
  27. //其实可以把这个html布局文件放在公网中,这样方便随时更新维护  例如 webview.loadUrl("www.xxxx.com/index.html");
  28. }
  29. //定义一个内部类,从java后台(可能是从网络,文件或者sqllite数据库) 获取List集合数据,并转换成json字符串,调用前台js代码
  30. private final class PersonPlugin{
  31. public void getPersonList(){
  32. List<Person> list = service.getPersonList();//获得List数据集合
  33. //将List泛型集合的数据转换为JSON数据格式
  34. try {
  35. JSONArray arr =new JSONArray();
  36. for(Person person :list)
  37. {
  38. JSONObject json =new JSONObject();
  39. json.put("id", person.getId());
  40. json.put("name", person.getName());
  41. json.put("mobile",person.getMobile());
  42. arr.put(json);
  43. }
  44. String JSONStr =arr.toString();//转换成json字符串
  45. webview.loadUrl("javascript:show('"+ JSONStr +"')");//执行html布局文件中的javascript函数代码--
  46. Log.i("MainActivity", JSONStr);
  47. } catch (Exception e) {
  48. // TODO: handle exception
  49. }
  50. }
  51. //打电话的方法
  52. public void call(String mobile){
  53. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ mobile));
  54. startActivity(intent);
  55. }
  56. }
  57. }
  1. package com.dazhuo.domain;
  2. public class Person {
  3. private Integer id;
  4. public Integer getId() {
  5. return id;
  6. }
  7. public Person(Integer id, String name, String mobile) {
  8. super();
  9. this.id = id;
  10. this.name = name;
  11. this.mobile = mobile;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getMobile() {
  23. return mobile;
  24. }
  25. public void setMobile(String mobile) {
  26. this.mobile = mobile;
  27. }
  28. private String name;
  29. private String mobile;
  30. }

  1. package com.dazhuo.service;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.dazhuo.domain.Person;
  5. public class PersonService {
  6. public List<Person> getPersonList()
  7. {
  8. List<Person> list =new ArrayList<Person>();
  9. list.add(new Person(32, "aa", "13675574545"));
  10. list.add(new Person(32, "bb", "13698874545"));
  11. list.add(new Person(32, "cc", "13644464545"));
  12. list.add(new Person(32, "dd", "13908978877"));
  13. list.add(new Person(32, "ee", "15908989898"));
  14. return list;
  15. }
  16. }

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. <script type="text/javascript">
  7. function show(jsondata){
  8. var jsonobjs = eval(jsondata);
  9. var table = document.getElementById("personTable");
  10. for(var y=0; y<jsonobjs.length; y++){
  11. var tr = table.insertRow(table.rows.length); //添加一行
  12. //添加三列
  13. var td1 = tr.insertCell(0);
  14. var td2 = tr.insertCell(1);
  15. td2.align = "center";
  16. var td3 = tr.insertCell(2);
  17. td3.align = "center";
  18. //设置列内容和属性
  19. td1.innerHTML = jsonobjs[y].id;
  20. td2.innerHTML = jsonobjs[y].name;
  21. td3.innerHTML = "<a href='javascript:Person.call(\""+ jsonobjs[y].mobile+ "\")'>"+ jsonobjs[y].mobile+ "</a>";
  22. }
  23. }
  24. </script>
  25. </head>
  26. <!-- js代码通过webView调用其插件中的java代码 -->
  27. <body onload="javascript:Person.getPersonList()">
  28. <table border="0" width="100%" id="personTable" cellspacing="0">
  29. <tr>
  30. <td width="20%">编号</td><td width="40%" align="center">姓名</td><td align="center">电话</td>
  31. </tr>
  32. </table>
  33. <a href="javascript:window.location.reload()">刷新</a>
  34. </body>
  35. </html>

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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

android中使用html作布局文件的更多相关文章

  1. Android 中使用 html 作布局文件

    在Android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...

  2. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  3. Android 自定义View及其在布局文件中的使用示例(二)

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...

  4. Android 自定义View及其在布局文件中的使用示例

    前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...

  5. android 非activity如何得到布局文件 (java文件中获取布局文件)

    Android中得到布局文件对象有两种方式第一种,在Activity所在类中this.getLayoutInflater().inflater(R.layout.布局文件名,null);第二种,在非A ...

  6. Android笔记——在布局文件中插入另一个布局文件

    假如有一个布局文件A.xml想把另外一个布局文件B.xml引进其布局,则可以通过下面的代码 <include layout="@layout/B" />

  7. android学习——Android Studio下创建menu布局文件

    一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...

  8. Android Studio下创建menu布局文件

    一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...

  9. 源代码解析Android中View的layout布局过程

    Android中的Veiw从内存中到呈如今UI界面上须要依次经历三个阶段:量算 -> 布局 -> 画图,关于View的量算.布局.画图的整体机制可參见博文 < Android中Vie ...

随机推荐

  1. vim时,ctrl+s了一下,程序僵死了

    刚刚在用vim的时候,按了ctrl+s,然后僵死了,ctrl+c.ctrl+d都没有反应. 不知怎么回事,差点就把它kill了,想探探究竟,网上查了一下,原来原来,这是个快捷键. ctrl+s 锁定屏 ...

  2. VC++编程中获取系统时间

    <span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...

  3. win32 sdk显示一个载入的位图的方法

    注:整理自网络文档 (1)加载位图 HANDLE LoadImage(HINSTANCE 来源实体,LPCTSTR 名称,UINT 位图类型, int 加载宽度,int 加载高度,UINT 加载方式) ...

  4. 转载:Comet:基于 HTTP 长连接的“服务器推”技术

    转自:http://www.ibm.com/developerworks/cn/web/wa-lo-comet/ 很多应用譬如监控.即时通信.即时报价系统都需要将后台发生的变化实时传送到客户端而无须客 ...

  5. 读书笔记:<世界是数字的>

    世界是数字的?第一次听到这个名词不由的感到惊讶,我有听过地球是海洋的,那世界不应该是人类共同拥有的吗或者也可以说世界是大自然的?为什么世界偏偏是数字的呢?我带着满心的疑问去看了那本<世界是数字的 ...

  6. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  7. 2012 Asia Chengdu Regional Contest

    Browsing History http://acm.hdu.edu.cn/showproblem.php?pid=4464 签到 #include<cstdio> #include&l ...

  8. YEBIS

    在往项目里加yebis做各种后处理 就是看起来很高大上的各种味精 我又热!泪!盈!眶!了 压缩包解开 有各种文档 恩哼~ 大概看了下找到sample build.... 直接就success了.... ...

  9. 网络(一),libevent客户端部分

    网络模块() 一.服务端: 暂时就以libevent模块,共享内存等下 .GS打开,首先创建4个libevent子线程,当然为每个线程设置连接通知回调函数,这个是基于sockpair的,然后再创建一个 ...

  10. API文档管理工具-数据库表结构思考.

    API文档管理工具-数据库表结构思考. PS: 管理工具只是为了方便自己记录API的一些基本信息,方便不同的开发人员 (App Developer, Restful API Developer)之间的 ...