Fast implementation of custom fonts in Android!

Source: Internet
Author: User

We all know that the default font in Android is bold, and most apps are used with this font, but we found that in most apps, individual places are very nice, such as the app's title bar, menu bar and other places, and how did they do it? There are two ways, the first is the picture to replace the text, and the second is today I want to teach you the custom font.

Development environment:

Android Studio 2.2.2

Compilesdkversion 25

Buildtoolsversion "25.0.0"

Minsdkversion 19

Targetsdkversion 25

Compile ' com.android.support:appcompat-v7:25.0.0 '

Tip: Be aware of changes to your development environment when using projects to avoid unnecessary waste of time.

1 Custom Fonts

When it comes to fonts, it's not hard to think of the various fonts we can choose when we use Office, which is what we need, and it's worth mentioning that Windows provides a lot of font files that can be found in C:\Windows\Fonts. Of course, we can also go to the Internet to download your favorite font files. Font file is in TTF format yo.

So let's start now, let's put the font file you want to use in the tool, and do the following:

(1) Create a new folder called assets, and then copy the font file to the inside

Stxingka. TTF is a font file

(2) We create a new class, called Fontcustom, to write the code:

 Public classFontcustom {//Fongurl is the name of the custom font category    Private StaticString Fongurl ="Stxingka. TTF"; //typeface is a font, here we create an object    Private StaticTypeface TF; /** * Set Font*/     Public StaticTypeface SetFont (context context) {if(TF = =NULL)        {            //Set your incoming custom font file to it and return backtf =Typeface.createfromasset (Context.getassets (), fongurl); }        returnTF; }}

(3) Create a new class called Mytextview inheritance TextView, overriding the construction method of 2 parameters

 Public class Mytextview extends TextView {    public  Mytextview (context context, AttributeSet attrs) {        Super (context, attrs);        Init (context);    }     /* *     * Initialize fonts     * @param context     */    privatevoid  Init (Context context) {        // set font style         settypeface (Fontcustom.setfont ( context));}    }

2 Using the Custom font class

We copy the path of Mytextview to Activity_main, replace the original TextView, and the path I have here is

Com.example.fengjun.fontdiy.MyTextView

To modify the code in Activity_main:

<?xml version="1.0"encoding="Utf-8"?><relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:id="@+id/activity_main"Android:layout_width="match_parent"Android:layout_height="match_parent"tools:context="com.example.fengjun.fontdiy.MainActivity"> <Com.example.fengjun.fontdiy.MyTextView android:layout_width="wrap_content"Android:layout_height="wrap_content"android:textsize="30SP"android:layout_centerinparent="true"Android:text="hello! I am a custom font"/></relativelayout>

The effect is cool, right? We only need 2 classes to complete the custom font, then where to use the custom font, the path to replace the original TextView is complete!

3 Summary

Custom fonts in our program in fact there is not much place, most of the time, we like to use images instead of TextView as a title name and other special places. If we show the text in the program, using a custom font, that is a great choice, it will give people a refreshing feeling.

Project: HTTP://PAN.BAIDU.COM/S/1PLHKGBH

Fast implementation of custom fonts in Android!

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.