歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中使用html作布局文件

Android中使用html作布局文件

日期:2017/3/1 11:12:39   编辑:Linux編程

在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>
Copyright © Linux教程網 All Rights Reserved