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 )"