在Android开发中,通常使用xml格式来描述布局文件。就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层。大部分企业,其实还是程序员自己动手布局。这样既浪费时间和精力,也未必能达到理想的效果。但是,在企业级的android开发中,使用html页面进行布局,也有很多的优势(例如:简单,大部分开发人员及美工都熟悉,方便统一进行更新,管理)。据笔者了解,已经有不少的公司在使用这种方式进行布局开发。这也可能是一种趋势。
下面,我将给出一个实例代码,供大家学习使用html页面给Android应用布局。
MainActivity.java
01 |
package com.dazhuo.ui; |
03 |
import java.util.List; |
05 |
import org.json.JSONArray; |
06 |
import org.json.JSONObject; |
08 |
import com.dazhuo.domain.Person; |
09 |
import com.dazhuo.service.PersonService; |
11 |
import Android.app.Activity; |
12 |
import Android.content.Intent; |
13 |
import Android.net.Uri; |
14 |
import Android.os.Bundle; |
15 |
import Android.util.Log; |
16 |
import Android.webkit.WebView; |
19 |
public class MainActivity extends Activity { |
20 |
private PersonService service; |
21 |
private WebView webview; |
23 |
public void onCreate(Bundle savedInstanceState) { |
24 |
super .onCreate(savedInstanceState); |
25 |
setContentView(R.layout.main); |
26 |
service = new PersonService(); |
27 |
webview = (WebView) this .findViewById(R.id.webView); |
28 |
webview.getSettings().setJavaScriptEnabled( true ); |
30 |
webview.addJavascriptInterface( new PersonPlugin() , "Person" ); |
32 |
webview.loadUrl( "file:///Android_asset/index.html" );//加载本地的html布局文件 |
36 |
private final class PersonPlugin{ |
37 |
public void getPersonList(){ |
38 |
List<Person> list = service.getPersonList(); |
41 |
JSONArray arr = new JSONArray(); |
42 |
for (Person person :list) |
44 |
JSONObject json = new JSONObject(); |
45 |
json.put( "id" , person.getId()); |
46 |
json.put( "name" , person.getName()); |
47 |
json.put( "mobile" ,person.getMobile()); |
51 |
String JSONStr =arr.toString(); |
52 |
webview.loadUrl( "javascript:show('" + JSONStr + "')" ); |
53 |
Log.i( "MainActivity" , JSONStr); |
54 |
} catch (Exception e) { |
60 |
public void call(String mobile){ |
61 |
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse( "tel:" + mobile)); |
62 |
startActivity(intent); |
Person.java
01 |
package com.dazhuo.domain; |
05 |
public Integer getId() { |
08 |
public Person(Integer id, String name, String mobile) { |
14 |
public void setId(Integer id) { |
17 |
public String getName() { |
20 |
public void setName(String name) { |
23 |
public String getMobile() { |
26 |
public void setMobile(String mobile) { |
30 |
private String mobile; |
PersonService.java
01 |
package com.dazhuo.service; |
03 |
import java.util.ArrayList; |
04 |
import java.util.List; |
06 |
import com.dazhuo.domain.Person; |
08 |
public class PersonService { |
09 |
public List<Person> getPersonList() |
12 |
List<Person> list = new ArrayList<Person>(); |
13 |
list.add( new Person( 32 , "aa" , "13675574545" )); |
14 |
list.add( new Person( 32 , "bb" , "13698874545" )); |
15 |
list.add( new Person( 32 , "cc" , "13644464545" )); |
16 |
list.add( new Person( 32 , "dd" , "13908978877" )); |
17 |
list.add( new Person( 32 , "ee" , "15908989898" )); |
index.html
01 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
04 |
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > |
05 |
< title >Insert title here</ title > |
06 |
< script type = "text/javascript" > |
07 |
function show(jsondata){ |
08 |
var jsonobjs = eval(jsondata); |
09 |
var table = document.getElementById("personTable"); |
10 |
for(var y=0; y< jsonobjs.length ; y++){ |
11 |
var tr = table .insertRow(table.rows.length); //添加一行 |
13 |
var td1 = tr .insertCell(0); |
14 |
var td2 = tr .insertCell(1); |
16 |
var td3 = tr .insertCell(2); |
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 >"; |
28 |
< body onload = "javascript:Person.getPersonList()" > |
29 |
< table border = "0" width = "100%" id = "personTable" cellspacing = "0" > |
31 |
< td width = "20%" >编号</ td >< td width = "40%" align = "center" >姓名</ td >< td align = "center" >电话</ td > |
34 |
< a href = "javascript:window.location.reload()" >刷新</ a > |