Textview does not support all HTML tags. If it is more complex, you can directly use the webview component.
HTML tags supported by Android textview
There is a lovely method onandroid.text.Html
Class,fromHtml()
,
That converts HTML intoSpannable
For use
WithTextView
.
However, the documentation does not
Stipulate what HTML tags are supported, which makes this method
Bit hit-or-miss. More importantly, it means that you cannot rely on
What it will support from release to release.
I have
Filed an issue
Requesting
That Google formally document what it intends to support. In
Interim, from a quick look at the source code, here's what seems
Be supported as of Android 2.1:
<A href = "...">
Define link content
<b>
Define bold text
B
Is the abbreviation of blod
<big>
Define text in a large font
<blockquote>
Reference Block tag
- Attribute:
- Common
--
General Attributes
- Cite
--
Uri of the referenced content
<br>
Define line feed
<cite>
Indicates the referenced Uri.
<dfn>
Define tags
Dfn
Yesdefining
Abbreviation of instance
<div
align="...">
<em>
Emphasize labels
Em
Is short for emphasis
<font size="..." color="..."
face="...">
<i>
Define italic text
src="...">
<p>
Section label, which can contain text, list, table, etc.
<small>
Define text in small font
<strike>
Define text in the strikethrough Style
Does not comply with the standard web design concept, do not approve of use.
Strike is short for strikethrough.
<strong>
Emphasis on labels
<sub>
Subscript tag
Sub
Is the abbreviation of subscript
<sup>
Superlabel
Sup
Is the abbreviation of superscript
<tt>
Define monospaced text
Disapproved.
This label is meaningless to Chinese
TT is teletype
Or monospaced text style
<u>
Define underlined text
U is underlined
Text Style
1. Use Android: textstyle = "bold" in the XML file"
2. But you cannot set Chinese to bold. The method to set Chinese to bold is as follows:
|
TextView
tv =
(
TextView)
findViewById(
R.
id
.
TextView01)
;
TextPaint tp =
tv.
getPaint
(
)
;
tp.
setFakeBoldText(
true)
;
|
Selecting, highlighting, Or styling portions of Text
You can highlight or style the formatting of strings or substrings
Of text in a textview object. There are two ways to do
This:
If you use a string resource, you can add some simple styling, such
As bold or italic using HTML notation. The currently supported tags
Are: B (BOLD), I (italic), u (underline), TT (monospace), big,
Small, sup (superscript), sub (subscript), and strike
(Strikethrough). So, for example, in RES/values/strings. xml you
Cocould declare this:
<Resource>
<String> id = "@ + ID/styled_welcome_message"> WE
Are
<B> <I> SO </I> </B>
Gglad to see you. </string>
</Resources>
To style text on the fly, or to add highlighting or more complex
Styling, you must use the spannable object as described
Next.
To style text on the fly, you must make sure the textview is using
Spannable storage for the text (this will always be true if
Textview is an edittext), retrieve its text with gettext (), and
Call setspan (object, Int, Int, INT), passing in a new style class
From the Android. Text. Style package and the selection
Range.
The following code snippet demonstrates creating a string with
Highlighted section, italic section, and bold Section, and adding
It to an edittext object.
// Get our edittext object.
Edittext vw = (edittext) findviewbyid (R. Id. Text );
// Set the edittext's text.
VW. settext ("italic, highlighted, bold .");
// If this were just a textview, We cocould do:
// VW. settext ("italic, highlighted, bold .",
Textview. buffertype. spannable );
// To force it to use spannable storage so styles can be
Attached.
// Or we cocould specify that in the XML.
// Get the edittext's internal text Storage
Spannable STR = VW. gettext ();
// Create our span sections, and assign a format to each.
Str. setspan (New stylespan (Android. Graphics. typeface. italic), 0, 7,
Spannable. span_exclusive_exclusive );
Str. setspan (New backgroundcolorspan (0xffffff00), 8, 19,
Spannable. span_exclusive_exclusive );
Str. setspan (New stylespan (Android. Graphics. typeface. Bold), 21,
Str. Length ()-1, spannable. span_exclusive_exclusive );
I sometimes have the case to arrange the image next to
Characters.
We can do it by putting textview and imageview
Layout.
But today I introduce the other way using only textview.
The following sample code is how to show the image next
Text.
(Show four image (left, top, right, bottom of text ))
Final textview =
(Textview) findviewbyid (R. Id. diet_log_label );
Final drawable icondrawable =
Getresources (). getdrawable (R. drawable. Icon );
Textview. setcompounddrawableswithintrinsicbounds (icondrawable,
Icondrawable, icondrawable, icondrawable );
// Or
Textview. setcompounddrawableswithintrinsicbounds (R. drawable. Icon,
R. drawable. Icon, R. drawable. Icon, R. drawable. Icon );
To show only left image, write
"Setcompounddrawableswithintrinsicbounds (icondrawable, null, null,
Null )"