如何在android中使用html作布局文件
在android開發(fā)中,通常使用xml格式來描述布局文件。就目前而言,熟悉android布局及美化的人員少之又少,出現(xiàn)了嚴(yán)重的斷層。大部分企業(yè),其實(shí)還是程序員自己動(dòng)手布局。這樣既浪費(fèi)時(shí)間和精力,也未必能達(dá)到理想的效果。但是,在企業(yè)級(jí)的android開發(fā)中,使用html頁(yè)面進(jìn)行布局,也有很多的優(yōu)勢(shì)(例如:簡(jiǎn)單,大部分開發(fā)人員及美工都熟悉,方便統(tǒng)一進(jìn)行更新,管理)。據(jù)筆者了解,已經(jīng)有不少的公司在使用這種方式進(jìn)行布局開發(fā)。這也可能是一種趨勢(shì)。
下面,我將給出一個(gè)實(shí)例代碼,供大家學(xué)習(xí)使用html頁(yè)面給android應(yīng)用布局。
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內(nèi)置瀏覽器對(duì)象
webview.getSettings().setJavaScriptEnabled(true);//啟用javascript支持
//添加一個(gè)js交互接口,方便html布局文件中的javascript代碼能與后臺(tái)java代碼直接交互訪問
webview.addJavascriptInterface(new PersonPlugin() , "Person");//new類名,交互訪問時(shí)使用的別名
// <body onload="javascript:Person.getPersonList()">
webview.loadUrl("file:///android_asset/index.html");//加載本地的html布局文件
//其實(shí)可以把這個(gè)html布局文件放在公網(wǎng)中,這樣方便隨時(shí)更新維護(hù) 例如 webview.loadUrl("www.xxxx.com/index.html");
}
//定義一個(gè)內(nèi)部類,從java后臺(tái)(可能是從網(wǎng)絡(luò),文件或者sqllite數(shù)據(jù)庫(kù)) 獲取List集合數(shù)據(jù),并轉(zhuǎn)換成json字符串,調(diào)用前臺(tái)js代碼
private final class PersonPlugin{
public void getPersonList(){
List<Person> list = service.getPersonList();//獲得List數(shù)據(jù)集合
//將List泛型集合的數(shù)據(jù)轉(zhuǎn)換為JSON數(shù)據(jù)格式
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();//轉(zhuǎn)換成json字符串
webview.loadUrl("javascript:show('"+ JSONStr +"')");//執(zhí)行html布局文件中的javascript函數(shù)代碼--
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";
//設(shè)置列內(nèi)容和屬性
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調(diào)用其插件中的java代碼 -->
<body onload="javascript:Person.getPersonList()">
<table border="0" width="100%" id="personTable" cellspacing="0">
<tr>
<td width="20%">編號(hào)</td><td width="40%" align="center">姓名</td><td align="center">電話</td>
</tr>
</table>
<a href="javascript:window.location.reload()">刷新</a>
</body>
</html>
- 通過Html網(wǎng)頁(yè)調(diào)用本地安卓(android)app程序代碼
- android textview 顯示html方法解析
- Android的TextView與Html相結(jié)合的具體方法
- android教程之textview解析帶圖片的html示例
- Android TextView顯示html樣式的文字
- Android開發(fā)之利用jsoup解析HTML頁(yè)面的方法
- Android TextView顯示Html類解析的網(wǎng)頁(yè)和圖片及自定義標(biāo)簽用法示例
- Android實(shí)現(xiàn)TextView顯示HTML加圖片的方法
- Android UI使用HTML布局方法實(shí)例
- Android編程獲取網(wǎng)址HTML代碼的方法
相關(guān)文章
利用Android從0到1實(shí)現(xiàn)一個(gè)流布局控件
新項(xiàng)目用到了一種全新布局,Android標(biāo)簽流式布局的功能,正好一直說給大家講自己定義控件的實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于利用Android從0到1如何實(shí)現(xiàn)一個(gè)流布局控件的相關(guān)資料,需要的朋友可以參考下2021-08-08Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫的實(shí)現(xiàn)方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android自定義狀態(tài)欄顏色與APP風(fēng)格保持一致的實(shí)現(xiàn)方法
我們知道iOS上的應(yīng)用,狀態(tài)欄的顏色總能與應(yīng)用標(biāo)題欄顏色保持一致,用戶體驗(yàn)很不錯(cuò),那安卓是否可以呢?下面小編給大家?guī)砹薃ndroid自定義狀態(tài)欄顏色與APP風(fēng)格保持一致的實(shí)現(xiàn)方法,跟著小編一起學(xué)習(xí)吧2016-10-10Android中Fab(FloatingActionButton)實(shí)現(xiàn)上下滑動(dòng)的漸變效果
這篇文章主要給大家介紹了Android中FloatingActionButton(簡(jiǎn)稱FAB)是如何實(shí)現(xiàn)上下滑動(dòng)的漸變效果,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,有需要的朋友們可以一起看看吧。2017-02-02從零開始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
這篇文章主要介紹了android實(shí)現(xiàn)的計(jì)算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧2014-02-02Android ViewPager實(shí)現(xiàn)圖片輪翻效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)圖片輪翻效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01android實(shí)現(xiàn)自動(dòng)關(guān)機(jī)的具體方法
android實(shí)現(xiàn)自動(dòng)關(guān)機(jī)的具體方法,需要的朋友可以參考一下2013-06-06DCloud的native.js調(diào)用系統(tǒng)分享實(shí)例Android版代碼
本文為大家分享了DCloud的native.js如何調(diào)用系統(tǒng)分享功能Android版的實(shí)例代碼,直接拿來就用2018-09-09