Androidstudio implements the method of loading font resources _android

Source: Internet
Author: User

Androidstudio to implement the method of loading font resources

The font format in Android doesn't always look perfect, so it requires us to use some aesthetically pleasing fonts, loaded in the same way as HTML fonts, and we need to load fonts to use fonts that are not available in Android.

First find the Assets folder in Androidstudio (under Serc/main/java, in the same file as res), locate the font file (*.ttf) and put it in the folder (for the sake of specification, of course, We can asseets to create a font folder to represent the fonts file. As shown in the figure:

After the resource is stored successfully, the font is loaded and used by the code:

where the Context.getassets () method is used to load the assets folder, "Opensans-regular.ttf" is the relative path of the font file, and attentive students can see that my assert has two identical font files,

One of them is under the font folder, and if you want to load this folder, you need to use:

typeface tf = Typeface.createfromasset (Getassets (), "Font/opensans-regular.ttf");

The use of this font in view shows the Settypeface.

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.