Trong phần “Hôm nay tôi đã học được gì về Android”, hóa ra bạn có thể hiển thị một chuỗi HTML trong Android TextView
. Tuy nhiên, cách tiếp cận này có những hạn chế lớn và có thể bạn sẽ muốn hiển thị HTML của mình trong một WebView
để thay thế
Bỏ qua vấn đề đó trong giây lát. nếu bạn muốn thử hiển thị một chuỗi HTML trong TextView
, bạn cần sử dụng phương pháp Android
TestProject2 Main TitleA sub-title
This is some html. Look, here\'s an underline.
Look, this is emphasized. And here\'s some bold.
This is a UL list:
- One
- Two
- Three
This is an OL list:
- One
- Two
- Three
// get our html content String htmlAsString = getString[R.string.html]; Spanned htmlAsSpanned = Html.fromHtml[htmlAsString]; // used by TextView // set the html content on the TextView TextView textView = [TextView] findViewById[R.id.textView]; textView.setText[htmlAsSpanned];
Chuỗi HTML của tôi
Trong mã ví dụ của tôi, tôi đã định nghĩa một
TestProject2 Main TitleA sub-title
This is some html. Look, here\'s an underline.
Look, this is emphasized. And here\'s some bold.
This is a UL list:
- One
- Two
- Three
This is an OL list:
- One
- Two
- Three
TestProject2 Main TitleA sub-title
This is some html. Look, here\'s an underline.
Look, this is emphasized. And here\'s some bold.
This is a UL list:
- One
- Two
- Three
This is an OL list:
- One
- Two
- Three
Như được hiển thị, nếu bạn định đặt nội dung HTML thành một chuỗi trong tệp XML như thế này, bạn cần đặt nó bên trong phần tử
TestProject2 Main TitleA sub-title
This is some html. Look, here\'s an underline.
Look, this is emphasized. And here\'s some bold.
This is a UL list:
- One
- Two
- Three
This is an OL list:
- One
- Two
- Three
TextView trông như thế nào
Với một tệp bố cục được xác định như thế này
và một
TestProject2 Main TitleA sub-title
This is some html. Look, here\'s an underline.
Look, this is emphasized. And here\'s some bold.
This is a UL list:
- One
- Two
- Three
This is an OL list:
- One
- Two
- Three
package com.alvinalexander.testproject2; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate[Bundle savedInstanceState] { super.onCreate[savedInstanceState]; setContentView[R.layout.activity_main]; // get our html content String htmlAsString = getString[R.string.html]; // used by WebView Spanned htmlAsSpanned = Html.fromHtml[htmlAsString]; // used by TextView // set the html content on a TextView TextView textView = [TextView] findViewById[R.id.textView]; textView.setText[htmlAsSpanned]; // WebView webView = [WebView] findViewById[R.id.webView]; // webView.loadDataWithBaseURL[null, htmlAsString, "text/html", "utf-8", null]; } }
chuỗi HTML được hiển thị trong tiện ích Android TextView
trông như thế này
Như bạn có thể thấy, TextView
không hiển thị đúng một số thành phần. Trên thực tế, bạn càng muốn sử dụng nhiều HTML, điều này càng trở thành một vấn đề, bởi vì TextView
có rất ít hỗ trợ để hiển thị HTML
Android WebView tốt hơn
Trên thực tế, các vấn đề về màn hình đủ tệ đến mức có lẽ tốt hơn hết bạn nên sử dụng WebView
thay vì TextView
. Nếu bạn loại bỏ các chú thích WebView
khỏi mã mà tôi đã trình bày và chú thích mã TextView
, bạn sẽ thấy rằng một WebView
hiển thị HTML tốt hơn nhiều
Bản tóm tắt
Vì vậy, trong khi bạn có thể hiển thị chuỗi HTML trong Android TextView
, câu hỏi sẽ trở thành, "Bạn có nên làm điều này không?"
Ghi chú. Đối với ví dụ này, tôi đã sử dụng
2 trên 14 và
3 trên 21 và kết quả được hiển thị trong trình giả lập Nexus 5