The correct way to use the font icon (iconfont) in Android _android

Source: Internet
Author: User

Font icon

The font icon is a font file (. ttf) that replaces the traditional PNG icon resource.

The advantages and disadvantages of using font icons are:

Advantages:

1. Can be highly customizable icon style (including size and color), especially for personal developers

2. Can reduce the size of the project and installation package (especially in your project has a lot of picture icon, the effect will be M-Class)

3. Can almost ignore screen size and resolution, do a better fit

4. Easy to use

......

Disadvantages:
1. Can only be some simple icon, can not replace such as background map, 9 map and other resources

2. Some words to explain the icon, picture resources will be a better choice

3. Higher requirements for the design, different icon may have different margins, this time to switch icon when you want to set the size

4. Because of the existence of margins there may be situations where the control cannot be filled

5. Unable to preview in real time in Android studio

Iconfont has many conveniences for client applications:

1. Free change size

2, free to modify the color

3, can add some visual effects such as: Shadow, rotation, transparency.

4, compared to the picture of the unit to save resources

Normal Add scheme

The first step: Copy the font file to the project assets directory;

The second step: Open the demo.html in the Iconfont directory, find the corresponding HTML entity character code;

Step Three: open res/values/strings.xml, add string value;

<string name= "Icons" > mobile </string>

Step Fourth: open Activity_main.xml, add string value to TextView:

<textview
  android:id= "@+id/like"
  android:layout_width= "Wrap_content"
  Wrap_content "
  android:text=" @string/icons "/>

Fifth Step: specify text for TextView:

Import Android.graphics.Typeface;

protected void OnCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_main);

  Typeface Iconfont = Typeface.createfromasset (Getassets (), "Iconfont/iconfont.ttf");
  TextView TextView = (TextView) Findviewbyid (r.id.like);
  Textview.settypeface (Iconfont);
}

Set the effect as follows

It's just so simple. But we found activity setTypeface it very unnecessary in the code. Because our entire application has many pages that need to set the font icon, this setting will have a lot of garbage code generated. Then we can solve the problem with a simple customization view

public class Iconfonttextview extends TextView {public  
  Iconfonttextview (context) {    
    super);    
     Init (context);  
  }  

  Public Iconfonttextview (context, AttributeSet attrs) {  
     Super (context, attrs);   
     Init (context);  
  }  

  Public Iconfonttextview (context, AttributeSet attrs, int defstyleattr) {    
     Super (context, attrs, DEFSTYLEATTR);    
     Init (context);  
  }    
   private void init (context context) {    
      typeface Iconfont = Typeface.createfromasset (Context.getassets (), iconfont/ Iconfont.ttf ");   
      Settypeface (Iconfont); 
 }

And then just change our layout file.

<com.xiaoming.liaoliao.view.iconfonttextview      
   android:layout_width= "wrap_content"   
   android:layout_ height= "Wrap_content" 
   android:textsize= "20DP"  
  android:textcolor= "@android: Color/holo_red_dark"  
  android:text= " mobile"/>

Other textview properties are normally used to resolve

Summarize

As you can see, using font icons to highly customize the size and color of icons is a particularly good message for individual developers, and no longer need to consider color and size when looking for icons. This is the correct way to use the font icon (Iconfont) in Android, and I hope this article will help Android developers, and if you have any questions, you can leave a message to communicate. Thank you for your support of the floats habitat community.

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.