Android TextView Advanced Display Techniques Example Summary _android

Source: Internet
Author: User

This example summarizes the Android TextView advanced display techniques. Share to everyone for your reference, specific as follows:

1. Custom Fonts

You can use the Settypeface (typeface) method to set the font for text in a text box, while Android typeface uses a TTF font file to set the font

So, we can put TTF font file in the program, use typeface in the program to set the font: The first step, in the assets directory under the new fonts directory, the TTF font file put here. In the second step, the program calls:

TEXTVIEWTV = (TextView) Findviewbyid (R.id.textview);
Assetmanagermgr=getassets ()//Get Assetmanager
typefacetf=typeface.createfromasset (Mgr, "Fonts/mini. TTF ");//Get typeface
tv.settypeface (TF) by path;//Set Font

The effect is shown in the following illustration:

2. Display a variety of colors of words

Android supports HTML-formatted strings, by invoking the html.fromhtml (str) method, you can convert the HTML-formatted string str.

Examples are as follows:

STRINGTEXTSTR1 = "<font color=\" #ffff00 \ "> If one day,</font><br>";
STRINGTEXTSTR2 = "<font color=\" #00ff00 \ "> I am tired of Here,</font><br>";
STRINGTEXTSTR3 = "<font color=\" #ff00ff > I will ride the Dream,</font><br> ";
STRINGTEXTSTR4 = "<font color=\" #00ffff > Fly to that belong to their own <br> world ......</font><br> ";
Tv.settext (html.fromhtml (TEXTSTR1+TEXTSTR2+TEXTSTR3+TEXTSTR4));

After the run effect is as follows:

3. Font Bold

The use of android:textstyle= "bold" in an XML layout file can be bold, but you cannot set the Chinese bold, and the Chinese will be bold by using the Textpaint imitation "bold" Set Setfakeboldtext to True. The sample code is as follows:

Tv.getpaint (). Setfakeboldtext (True);

The effect is as follows:

4. Add Shadow

Use a series of android:shadowxxx properties in an XML layout file to add a setting shadow. Specifically: Shadowcolor sets the shadow color, SHADOWDX sets the shadow horizontal offset, Shadowdy sets the shadow vertical offset, and Shadowradius sets the shadow radius.

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 Picture

There are two ways to insert a picture, the first of which is to use the HTML-formatted string above, but the conversion is a bit cumbersome. You need to use the Imagegetter class to convert the src attribute of the picture. The sample code is as follows:

Stringimgstr = " 
 

The second approach is to use a series of android:drawablexxx properties in the XML layout file to insert the picture. Drawablebottom is to draw the specified image at the bottom of the text in the text box, Drawableleft to draw the specified image to the left of the text in the text box, and drawableright to draw the specified image to the right of the text in the text box. Drawabletop to draw the specified image at the top of the text in the text box, and drawablepadding to set the spacing between the text and the image in the text box. Sample code:

android:drawablebottom= "@drawable/sidai"

After you insert a picture, the results appear as follows:

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Layout Layout Skills Summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course" , Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.