當前位置:首頁 » 安卓系統 » androidhtml圖片

androidhtml圖片

發布時間: 2025-05-29 23:30:58

⑴ android中如何在textview中加入html

具體代碼如下:

Android中的TextView,本身就支持部分的Html格式標簽。這其中包括常用的字體大小顏色設置,文本鏈接等。使用起來也比較方便,只需要使用Html類轉換一下即可。比如:

textView.setText(Html.fromHtml(str));

然而,有一種場合,默認支持的標簽可能不夠用。比如,我們需要在textView中點擊某種鏈接,返回到應用中的某個界面,而不僅僅是網路連接,如何實現?

經過幾個小時對android中的Html類源代碼的研究,找到了解決辦法,並且測試通過。

⑵ 如何在android中使用html作布局文件

在android開發中,通常使用xml格式來描述布局文件。就目前而言,熟悉android布局及美化的人員少之又少,出現了嚴重的斷層。大部分企業,其實還是程序員自己動手布局。這樣既浪費時間和精力,也未必能達到理想的效果。但是,在企業級的android開發中,使用html頁面進行布局,也有很多的優勢(例如:簡單,大部分開發人員及美工都熟悉,方便統一進行更新,管理)。據筆者了解,已經有不少的公司在使用這種方式進行布局開發。這也可能是一種趨勢。
下面,我將給出一個實例代碼,供大家學習使用html頁面給android應用布局。
java代碼
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);
}
}
}
Java代碼
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;
}
Java代碼
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;
}
}
Html代碼
<!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、先示例圖片

2、操作代碼

package com.nekocode.xue.utils;

import java.io.File;

import java.io.FileOutputStream;

import java.util.ArrayList;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import com.androidquery.AQuery;

import com.androidquery.callback.AjaxCallback;

import com.androidquery.callback.AjaxStatus;

import com.nekocode.xue.PublicData;

import com.nekocode.xue.PublicData.Subscribe;

public class HtmlStorageHelper {

private String URL = "http://eproject.sinaapp.com/fetchurl.php/getcontent/";

private PublicData pd;

private AQuery aq;

private SQLiteDatabase mDB;

private String mDownloadPath;


public HtmlStorageHelper(Context context) {

pd = PublicData.getInstance();

aq = new AQuery(context);

mDB = context.openOrCreateDatabase("data.db", Context.MODE_PRIVATE, null);

mDB.execSQL("create table if not exists download_html(_id INTEGER PRIMARY KEY AUTOINCREMENT, content_id TEXT NOT NULL, title TEXT NOT NULL)");


mDownloadPath = pd.mAppPath + "download/";

File dir_file = new File(pd.mAppPath + "download/");

if(!dir_file.exists())

dir_file.mkdir();

}


public void saveHtml(final String id, final String title) {

if(isHtmlSaved(id))

return;


aq.ajax(URL+id, String.class, new AjaxCallback<String>() {

@Override

public void callback(String url, String html, AjaxStatus status) {

File dir_file = new File(mDownloadPath + id);

if(!dir_file.exists())

dir_file.mkdir();


Pattern pattern = Pattern.compile("(?<=src=")[^"]+(?=")");

Matcher matcher = pattern.matcher(html);

StringBuffer sb = new StringBuffer();

while(matcher.find()){

downloadPic(id, matcher.group(0));

matcher.appendReplacement(sb, formatPath(matcher.group(0)));

}

matcher.appendTail(sb);

html = sb.toString();


writeHtml(id, title, html);

}

});

}


private void downloadPic(String id, String url) {

File pic_file = new File(mDownloadPath + id + "/" + formatPath(url));

aq.download(url, pic_file, new AjaxCallback<File>() {

@Override

public void callback(String url, final File file, AjaxStatus status) {

}

});

}


private void writeHtml(String id, String title, String html) {

File html_file = new File(mDownloadPath + id + "/index.html");

FileOutputStream fos = null;

try {

fos=new FileOutputStream(html_file);

fos.write(html.getBytes());

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

fos.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}


ContentValues values = new ContentValues();

values.put("content_id", id);

values.put("title", title);

mDB.insert("download_html", "_id", values);

}


public boolean isHtmlSaved(String id) {

File file = new File(mDownloadPath + id);

if(file.exists()) {

file = new File(mDownloadPath + id + "/index.html");

if(file.exists())

return true;

}

deleteHtml(id);

return false;

}


public String getTitle(String id) {

Cursor c = mDB.rawQuery("select * from download_html where content_id=?", new String[]{id});

if(c.getCount() == 0)

return null;


c.moveToFirst();

int index1 = c.getColumnIndex("title");


return c.getString(index1);

}


public ArrayList<Subscribe> getHtmlList() {

Cursor c = mDB.rawQuery("select * from download_html", null);

ArrayList<Subscribe> list = new ArrayList<Subscribe>();

if(c.getCount() != 0) {

c.moveToFirst();

int index1 = c.getColumnIndex("content_id");

int index2 = c.getColumnIndex("title");


while (!c.isAfterLast()) {

String id = c.getString(index1);

if(isHtmlSaved(id)) {

Subscribe sub = new Subscribe(

id,

c.getString(index2),

Subscribe.FILE_DOWNLOADED

);

list.add(sub);

}


c.moveToNext();

}

}


return list;

}


public void deleteHtml(String id) {

mDB.delete("download_html", "content_id=?", new String[]{id});

File dir_file = new File(mDownloadPath + id);

deleteFile(dir_file);

}

private void deleteFile(File file) {

if (file.exists()) { // 判斷文件是否存在

if (file.isFile()) { // 判斷是否是文件

file.delete(); // delete()方法 你應該知道 是刪除的意思;

} else if (file.isDirectory()) { // 否則如果它是一個目錄

File files[] = file.listFiles(); // 聲明目錄下所有的文件 files[];

for (int i = 0; i < files.length; i++) { // 遍歷目錄下所有的文件

this.deleteFile(files[i]); // 把每個文件 用這個方法進行迭代

}

}

file.delete();

} else {

//

}

}


private String formatPath(String path) {

if (path != null && path.length() > 0) {

path = path.replace("\", "_");

path = path.replace("/", "_");

path = path.replace(":", "_");

path = path.replace("*", "_");

path = path.replace("?", "_");

path = path.replace(""", "_");

path = path.replace("<", "_");

path = path.replace("|", "_");

path = path.replace(">", "_");

}

return path;

}

}

3、這段代碼簡單修改就可以用到自己的項目中了

⑷ Android開發中對顯示HTML內容的幾種方式

首先,Android中顯示Html內容,有3中方式:(目前我用到的有這3種)
1、可以利用Android原生的Html.fromHtml(str, imageGetter, tagHandler)來進行顯示。(不過,我這邊用了,即使加了頁面載入動畫,還是覺得非常慢,有大量圖片,會導致OOM;如果圖片不多的話,可以考慮)
2、利用第三方插件HtmlTextView。
GitHub地址:https://github.com/PrivacyApps/html-textview
圖片載入很順暢,使用方法也非常簡單,不過,有兩個注意事項:
(1)其中,HtmlHttpImageGetter有3個構造函數,可以根據自己的情況選擇。
(2)載入大量圖片的時候,會導致OOM內存溢出。針對於這個情況,HtmlHttpImageGetter有一個壓縮圖片的方法可以調用,可以進去查看它的公共方法。(不過,我這邊顯示的圖片過大,每張1M左右,並且一下子有幾十張,即使設置了壓縮圖片,還是會導致OOM問題,目前還沒解決,有大神知道咋弄的,拜託指點一下,非常感謝!)
對了,這個第三方插件的基本用法,點擊上面的連接,進去一看就知道了,很簡單。
3、第三種,是我沒辦法的情況下想的:把html標簽里的內容利用正則表達式拿出來,其中,文本內容用一個TextView代替,<img>標簽圖片用一個ImageView代替,其他相關的標簽自行選擇替換。說白了,就是創建一個個的TextView以及ImageView填充到布局裡(LinearLayout之類的)。要說明的是:其中圖片顯示用Glide來實現。目前我這邊測試的情況還是可以的,載入的速度非常快,也沒因內存問題導致APP崩潰。

熱點內容
華為liteos編譯 發布:2025-05-30 13:25:52 瀏覽:846
北森編程題 發布:2025-05-30 12:53:49 瀏覽:787
無法打開ftp 發布:2025-05-30 12:47:11 瀏覽:663
文件夾的資料 發布:2025-05-30 12:06:07 瀏覽:73
蘋果手機伺服器地址哪裡獲取 發布:2025-05-30 11:46:25 瀏覽:752
安卓系統開源谷歌怎麼控制的 發布:2025-05-30 11:43:30 瀏覽:368
5m上傳速度 發布:2025-05-30 11:43:25 瀏覽:241
c語言集合的並運算 發布:2025-05-30 11:41:11 瀏覽:826
雲學生伺服器 發布:2025-05-30 11:15:20 瀏覽:154
瑞恩源碼 發布:2025-05-30 11:02:33 瀏覽:354