Using the Fontawesome icon set in an Android project makes it easy to adapt to a variety of screen resolutions without having to create a new resource file in a variety of different resolution folders. Using fonts is a clever way to render images as fonts, which were previously set to Android:background, and now only need to be set android:text, and can be adapted to different resolutions.
When you set a font in an XML file, you only need to specify the Android:typeface property, but this value only supports several fonts that are built into the system.
Import Fontawesome fonts, you need to import third-party fonts in your project. is to create a new fonts directory in the assets directory, and then import the TTF file that we need. Take Fontawesome as an example, the address of Git clone fontawesome is Https://github.com/FortAwesome/Font-Awesome.git, After download, copy the Fontawesome-webfont.ttf file in the extracted fonts directory to the fonts directory.
To create a helper class:
1 Public classFontmanager {2 Public Static FinalString root = "fonts/";3 Public Static FinalString fontawesome = root + "Fontawesome-webfont.ttf";4 5 Public StaticTypeface Gettypeface (context context, String font) {6 returnTypeface.createfromasset (context.getassets (), font);7 }8 9}
Layout file:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:id= "@+id/root"7 Tools:context=". Mainactivity ">8 9 <LinearLayoutTen Android:layout_width= "Match_parent" One Android:layout_height= "Wrap_content" A Android:layout_alignparentbottom= "true" - android:orientation= "Horizontal"> - the <TextView - Android:id= "@+id/tv1" - Android:layout_width= "0DP" - Android:layout_height= "45DP" + Android:layout_weight= "1" - android:gravity= "Center" + Android:textcolor= "#00BFFF" A android:textsize= "22SP" at Android:typeface= "Monospace" - Android:text= "@string/ambulance"/> - - <TextView - Android:id= "@+id/tv2" - Android:layout_width= "0DP" in Android:layout_height= "45DP" - Android:layout_weight= "1" to android:gravity= "Center" + android:textsize= "22SP" - Android:textcolor= "#87CEFA" the Android:text= "@string/bicycle"/> * $ <TextViewPanax Notoginseng Android:id= "@+id/tv3" - Android:layout_width= "0DP" the Android:layout_height= "45DP" + Android:layout_weight= "1" A android:gravity= "Center" the android:textsize= "22SP" + Android:textcolor= "#00CED1" - Android:text= "@string/bus"/> $ <TextView $ Android:id= "@+id/tv4" - Android:layout_width= "0DP" - Android:layout_height= "45DP" the Android:layout_weight= "1" - android:gravity= "Center"Wuyi android:textsize= "22SP" the Android:textcolor= "#CD5C5C" - Android:text= "@string/train"/> Wu </LinearLayout> - </Relativelayout>
Strings.xml:
1 <Resources>2 <stringname= "App_name">Fontawesomemotion</string>3 <stringname= "Ambulance">& #xf0f9;</string>4 <stringname= "Bus">& #xf207;</string>5 <stringname= "Bicycle">& #xf206;</string>6 <stringname= "Train">& #xf238;</string>7 </Resources>
In the Strings.xml resource file, this resource string represents a different icon. You can query the specific icon by http://fortawesome.github.io/Font-Awesome/cheatsheet/.
1 Packagecom.app.motiongear.fontawesomemotion;2 3 ImportAndroid.graphics.Typeface;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 Importandroid.widget.RelativeLayout;8 ImportAndroid.widget.TextView;9 Ten ImportCom.app.motiongear.utils.FontManager; One A ImportJava.lang.reflect.Type; - - Public classMainactivityextendsappcompatactivity { the - relativelayout Mcontainer; - - @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); A FinalTypeface tf = Fontmanager.gettypeface ( This, fontmanager.fontawesome); atMcontainer = (relativelayout) This. Findviewbyid (r.id.root); - for(inti = 0; I < Mcontainer.getchildcount (); i++) { -View CurrentView =Mcontainer.getchildat (i); - if(CurrentViewinstanceofTextView) { - ((TextView) currentview). Settypeface (TF); - } in } - to } +}
The actual display results are as follows:
Using Fontawesome fonts in Android projects