Android UI Design Series HTML tag implementation TextView set Chinese font bold effect (6) _android

Source: Internet
Author: User

Engaged in software development are aware of the various needs of the project have, and sometimes the needs of a variety of exotic flowers really let the people surprised, in order to achieve the needs of these wonderful flowers we often hard to sleep, I now have a project should be considered a wonderful demand for it, the demand is this: Make some text bold in a piece of text
It doesn't look too difficult and a bit pediatrics, because there's a property in the TextView that is Android:textstyle, and one of the values is bold, which means that the text you want to display is bold, So I added this property to the TextView control, and the code was as follows:

<textview 
 android:layout_width= "fill_parent" 
 android:layout_height= "Wrap_content" 
 android: Layout_margin= "5dip" 
 android:gravity= "center" 
 android:text= "Chinese bold? Chiness is bolded? " 
 Android:textcolor= "#000000" 
 android:textstyle= "bold" 
 android:textsize= "18sp"/> 

Run the program to test the effect, it is regrettable that the English and the number can be bold, but the Chinese will have no effect. And then think of the use of Android with the Html.fromhtml method (I was engaged in the development of Java, the Web is more familiar with, hehe), because in the web development using the strong tag or a B tag can be used to achieve the bold text effect, And then I was in TextView's SetText () Methods using the Html.fromhtml method, after the operation found no bold effect, and then tried to reinstall APK Test still did not realize I want the Chinese bold effect, and then on the internet to ask Baidu, because I prefer to ask Niang, hehe
Other implementations of Chinese bold are viewed on the web, except using the Android:textstyle method to invoke Setfakeboldtext (true) of the Textpaint object, in addition to the TextView Textpaint object. can achieve the effect of bold text, I tried a bit, the code is as follows:

title = (TextView) Findviewbyid (r.id.title); 
Title.getpaint (). Setfakeboldtext (True); 

This can be achieved in both Chinese and English at the same time bold, the implementation of the requirements on the text bold effect, but the problem, I need to implement the text bold is in a paragraph of words, but not all the text need to be bold, so the problem comes, Although the above method can be used to make the Chinese bold, but all the text is bold, in order to achieve some of the text bold, I am fortunate to have to bold text to use a separate TextView implementation, do not need to bold on the use of another textview, so basically achieve the needs of the desired effect.
Although the effect is achieved, but the problem is still in my heart, later, inadvertently look at a control source code found in its comments have a bold effect of the use of the <tt> tag, I immediately came to the interest in the mind that may be the label can be implemented on the part of the bold, so quickly test, The code is as follows:

title = (TextView) Findviewbyid (r.id.title); 
Title.settext (html.fromhtml) ("<b>china China <tt> China </tt></b>china is really great!" ")); 

Run the program, sure enough, and is realized in a textview in a part of the text to achieve the bold, oh, happy bad, so hurriedly record down, for the future needs of children's shoes to leave a convenient bar
Summarize The Bold way:

Detailed code See: http://www.jb51.net/article/85965.htm

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.