Android app Development uses third-party fonts

Source: Internet
Author: User

Android native fonts may not let the UI sister paper favor, it is too ugly, so the UI sister paper used third-party fonts as the font style of the app, this blog is to summarize in the development of Android app how to use third-party fonts.

first, there must be a third-party font library , where the font library file is black_simplified. TTF, create a new front folder under the Android Assert directory and place the font library file under the front directory, which is/assert/front/black_simplified. TTF

Here's a summary of how it's easiest to use third-party fonts in your app. In TextView, for example, there is a method in the API interface called settypeface (Typeface obj), which is the interface to set the font style of the TextView display, then how to get the Typeface object, after viewing the API can be Typeface.creatfromassert (obj) way to get, the entire process of using a third-party font for a single TextView is as follows:

public class Mainactivity extends Activity {private Typeface text_type;p rivate TextView mTv; @Overrideprotected void oncr Eate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Load Custom Fonts Try{text_type = Typeface.createfromasset (Getassets (), "fronts/black_simplified". TTF "); }catch (Exception e) {log.i ("mainactivity", "Loading third-party fonts failed. ") ; Text_type = null;} MTv = (TextView) Findviewbyid (R.ID.TV1); if (text_type! = null) {mtv.settypeface (text_type);} Mtv.settext ("Android Program Development");}}

You can see the effect after you run it. But isn't that a lot of trouble?! To set the font style for each control that needs to use a third-party font. The workaround here is to rewrite the controls that require the use of third-party fonts , TextView for example, and of course there are many controls on Android that can display fonts, such as button, EditText. Android development often needs to implement its own application object, because it is global, is the first module executed by the program, but also facilitates data sharing, so, the initialization of the font is placed in our custom application subclass MyApp. The code snippet is as follows:

Initializes the custom typeface in the Myapp.java OnCreate function:

@Overridepublic void OnCreate () {//load custom Font try{text_type = Typeface.createfromasset (Getassets (), "Fronts/black_ Simplified. TTF "); }catch (Exception e) {log.i ("MyApp", "Loading third-party fonts failed.") ") ; Text_type = null;} Super.oncreate ();}

In the <application> in the Androidmanifest.xml file, note that the name attribute is a custom application subclass:

<application        android:name= "com.example.androidfronttypeface.MyApp"        ......</application>

Custom TextView:

public class Myfronttextview extends TextView {public Myfronttextview (context context) {super (context); Settypeface ();} Public Myfronttextview (context context, AttributeSet Attrs) {Super (context, attrs); Settypeface ();} Public Myfronttextview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Settypeface ( ) ;} private void Settypeface () {//If the custom typeface initialization fails, use the native typefaceif (Myapp.text_type = = null) {Settypeface (Gettypeface () ) ;} Else{settypeface (Myapp.text_type);}}}
You need to reference the custom TextView in the layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent " >    <com.example.androidfronttypeface.myfronttextview        android:id= "@+id/tv1"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Android app development"/></ Relativelayout>

This is probably the case, if a button or other control needs to use a third-party font, the same is true.

Source Reference








Android app Development uses third-party fonts

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.