English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Advanced Display Techniques of Android TextView Example Summary

This article summarizes the advanced display skills of Android TextView. Share it with everyone for reference, as follows:

1. Custom font

You can use the setTypeface(Typeface) method to set the font of the text in the text box, and Android's Typeface uses TTF font files to set the font

Therefore, we can put TTF font files in the program and use Typeface to set the font in the program: first, create a fonts directory under the assets directory and put the TTF font file here. Second, call in the program:

TextViewtv = (TextView)findViewById(R.id.textView);
AssetManagermgr=getAssets();//Get AssetManager
Typefacetf=Typeface.createFromAsset(mgr, "fonts/mini.TTF");//Get Typeface by path
tv.setTypeface(tf);//Set font

The effect is shown in the figure below:

2. Display text in multiple colors

Android supports html formatted strings, and can convert html formatted string str by calling Html.fromHtml(str) method.

The example is as follows:

StringtextStr1 = "<font color=\"#ffff00\">If one day,/font><br>";
StringtextStr2 = "<font color=\"#00ff00\">I am tired of this place,/font><br>";
StringtextStr3 = "<font color=\"#ff00ff\">I will ride on the dream,/font><br>";
StringtextStr4 = "<font color=\"#00ffff\">Fly towards that world of my own<br>.../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, using android:textStyle="bold" can set English text to bold, but it cannot set Chinese text to bold. The method to set Chinese text to bold is: use the仿"bold"setting of TextPaint setFakeBoldText to true. The example code is as follows:

tv.getPaint().setFakeBoldText(true);

Effect as follows:

4. Add shadow

Using a series of android:shadowXXX properties in the xml layout file can add shadow settings. Specifically: shadowColor sets the shadow color; shadowDx sets the horizontal offset of the shadow; shadowDy sets the vertical offset of the shadow; shadowRadius sets the shadow radius.

Example code:

android:shadowColor="#ffffff"
android:shadowDx="15.0"
android:shadowDy="5.0"
android:shadowRadius="2.5"

Display effect as follows:

5. Insert image

There are two methods to insert images, the first method is to use the html format string mentioned above, but the conversion is a bit麻烦. It needs to use the ImageGetter class to convert the src attribute of the image. Example code as follows:

String imgStr = "<img src=\""+R.drawable.sidai+"\"/>";
Html.ImageGetter imageGetter = new Html.ImageGetter() {
   public Drawable getDrawable(String arg0) {
     // TODO Auto-generated method stub
     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 images by using a series of android:drawableXXX properties in the xml layout file. Specifically: drawableBottom draws the specified image at the bottom of the text box text; drawableLeft draws the specified image on the left side of the text box text; drawableRight draws the specified image on the right side of the text box text; drawableTop draws the specified image at the top of the text box text; drawablePadding sets the spacing between the text and the image inside the text box. Example code:

android:drawableBottom="@drawable"/sidai

The display effect after inserting the image is as follows:

Readers who are interested in more content related to Android can check the special topics on this website: 'Summary of Android View View Skills', 'Summary of Android Layout Layout Skills', 'Summary of Android Graphics and Image Processing Skills', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Skills and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Skills (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', and 'Summary of Android Widget Usage'

I hope the content described in this article will be helpful to everyone's Android program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting. Provide relevant evidence, and once verified, the website will immediately delete the content suspected of infringement.

You May Also Like