Android uses a custom font

Source: Internet
Author: User

Entire project to use a third-party font first place the font file under the Assets folder

Because the whole project is going to use a third-party font here I rewrote the TextView Button EditText three controls

Take TextView as an example code to replace inheritance as other controls

 Public classCustomtextviewextendsTextView { PublicCustomtextview (Context context) {Super(context);    Init (context); }     PublicCustomtextview (Context context, AttributeSet attrs) {Super(context, attrs);    Init (context); }     PublicCustomtextview (context context, AttributeSet attrs,intDefsyle) {        Super(context, attrs, Defsyle);    Init (context); }    /*** * Set Font * *@return     */     Public voidinit (Context context) {Settypeface (Fontcustom.setfont (context)); }}
View Code
 Public classFontcustom {StaticString Fongurl = "Fonts/fzltxh_gbk.ttf"; StaticTypeface TF; /*** * Set Font * *@return     */     Public StaticTypeface SetFont (context context) {if(tf==NULL) {tf=Typeface.createfromasset (Context.getassets (), fongurl); }        returnTF; }}
View Code

How to use

< Customtextview                 Android:layout_width = "Match_parent"                 android:layout_height= "Wrap_content"                  android:text= "I am a custom font"                 android:textcolor= "@color/login_font_hit"                android:textsize= " 14.0sp "/>
View Code

This enables the unified style of the project font

Android uses a custom font

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.