Advanced display of TextView

Source: Internet
Author: User

 1.Custom font
You can use the setTypeface (Typeface) method to set the text font in the text box, while the android Typeface uses the TTF font file to set the font.
Therefore, we can put the TTF font file in the program and use Typeface in the program to set the font: Step 1: Create a fonts directory under the assets Directory and put the TTF font file here. Step 2: Call in the program:
TextViewtv = (TextView) findViewById (R. id. textView );
AssetManagermgr = getAssets (); // get AssetManager
Typefacetf = Typeface. createFromAsset (mgr, "fonts/mini. TTF"); // obtain Typeface Based on the path
TV. setTypeface (tf); // set the font
Shows the effect:
 

 
2.Display words of multiple colors
Android supports html strings. You can call the Html. fromHtml (str) method to convert the string 'str' in html format.
Example:
StringtextStr1 = "<font color = \" # ffff00 \ "> if one day, </font> <br> ";
StringtextStr2 = "<font color = \" #00ff00 \ "> I'm tired of this, </font> <br> ";
StringtextStr3 = "<font color = \" # ff00ff \ "> I will ride on the dream, </font> <br> ";
StringtextStr4 = "<font color = \" # 00ffff \ "> fly to your own <br> world ...... </Font> <br> ";
TV. setText (Html. fromHtml (textStr1 + textStr2 + textStr3 + textStr4 ));
The effect after running is as follows:
 

 
3.Bold font
In the xml layout file, you can use android: textStyle = "bold" to set English to bold, but you cannot set Chinese to bold. The method to set Chinese to bold is: set setFakeBoldText to true using TextPaint in the form of "bold. The sample code is as follows:
TV. getPaint (). setFakeBoldText (true );
The effect is as follows:


 
4.Add shadow
You can use a series of android: shadowXXX attributes in the xml layout file to add and set shadows. Specifically, shadowColor is used to set the shadowColor, shadowDx is used to set the horizontal shadow offset, shadowDy is used to set the vertical shadow offset, and shadowRadius is used to set the shadowRadius.
Sample Code:
Android: shadowColor = "# ffffff"
Android: shadowDx = "15.0"
Android: shadowDy = "5.0"
Android: shadowRadius = "2.5"
The display effect is as follows:
 

 
5.Insert Image
There are two ways to insert an image. The first method is to use the preceding html format string, but the conversion is a little troublesome. You need to use the ImageGetter class to convert the src attribute of an image. The sample code is as follows:
StringimgStr = " ";
Html. ImageGetterimageGetter = new Html. ImageGetter (){
Public Drawable getDrawable (Stringarg0 ){
// TODO Auto-generated methodstub
Int id = Integer. parseInt (arg0 );
Drawable draw = getResources (). getDrawable (id );
Draw. setBounds (10, 10,228,300 );
Return draw;
}
};
TV. append (Html. fromHtml (imgStr, imageGetter, null ));
The second method is to insert an image using the android: drawableXXX attribute in the xml layout file. Specifically, drawableBottom draws the specified image at the bottom of the text in the text box; drawableLeft draws the specified image on the left of the text in the text box; drawableRight draws the specified image on the right of the text in the text box; drawableTop is used to draw the specified image at the top of the text in the text box. drawablePadding sets the spacing between the text and the image in the text box. Sample Code:
Android: drawableBottom = "@ drawable/sidai"
The following figure shows the effect of the inserted image:





From Peking University-Google Android lab

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.