Android API Typeface code demonstration)

Source: Internet
Author: User
Tags italic font
Previously, an Android developer in Beijing initiated an initiative to translate Android APIs, which brought together the power of every Chinese Android developer to grow, it provides a bridge for developers who generally cannot fully understand the original Android API. After all, language should not be a hindrance to developers.

Typeface class specifies the fonts and inherent features of fonts in detail. When drawing (and measuring), it is helpful to use a paint brush, especially when using textSize, textSkewX, and textScaleX to specify text display.

Demo (for personal originality, of course, some domestic/foreign examples are referenced when encountering problems ):

(1) Create Layout

// Create a linear Layout

LinearLayout linearLayout =NewLinearLayout (This);

// Set the linear layout to the vertical direction

LinearLayout. setOrientation (LinearLayout.VERTICAL);

// Use this linear layout as a view

SetContentView (linearLayout );

(2) For normal Fonts

// Normal font

Normal =NewTextView (This);

// Set the font content. Note the following:AndroidThe font settings are mainly applicable to the Latin series. Chinese characters are not supported currently.

Normal. setText ("Normal Font FYI ");

// Set the font size

Normal. setTextSize (20366f );

// Set the font to the default value. The font is normal.

Normal. setTypeface (Typeface.DEFAULT, Typeface.NORMAL);

// Add the font and display it to the layout linearLayout.

LinearLayout. addView (normal,NewLinearLayout. LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

(3) For bold fonts

// Bold font

Bold =NewTextView (This);

Bold. setText ("Bold Font FYI ");

Bold. setTextSize (20366f );

// Set the font color to blue

Bold. setTextColor (Color.BLUE);

// Set the font to bold by default and bold

Bold. setTypeface (Typeface.DEFAULT_BOLD, Typeface.BOLD);

LinearLayout. addView (bold,NewLinearLayout. LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

(4) italic Fonts

// Italic font

Italic =NewTextView (This);

Italic. setTextSize (20f );

Italic. setText ("Italic Font FYI ");

// Set the font color to red

Italic. setTextColor (Color.RED);

// Set the font to an equal-width font or italic font.

Italic. setTypeface (Typeface.MONOSPACE, Typeface.ITALIC);

LinearLayout. addView (italic,NewLinearLayout. LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

(5) bold italic Fonts

// Bold italic font

Italic_bold =NewTextView (This);

Italic_bold.setTextSize (20f );

Italic_bold.setText ("Italic & Bold Font FYI ");

// Set the font color to yellow

Italic_bold.setTextColor (Color.YELLOW);

// Set the font to an equal-width font or italic font.

Italic_bold.setTypeface (Typeface.MONOSPACE, Typeface.BOLD_ITALIC);

LinearLayout. addView (italic_bold,NewLinearLayout. LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

(6) Chinese imitation "bold"

//AndroidChinese font of fonts

Chinese =NewTextView (This);

Chinese. setText ("chinese bold Display Effect ");

// Set the font color

Chinese. setTextColor (Color.MAGENTA);

Chinese. setTextSize (20366f );

Chinese. setTypeface (Typeface.DEFAULT_BOLD, Typeface.BOLD);

// Set setFakeBoldText to true using the text in bold of TextPaint. Currently, the "italic-like" method cannot be used.

Tp = chinese. getPaint ();

Tp. setFakeBoldText (True);

LinearLayout. addView (chinese,NewLinearLayout. LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

(7) create custom Fonts

// Custom font

Custom =NewTextView (This);

// The MgOpenCosmeticaBold. ttf font is stored in assets/font/path.

Typeface = Typeface.CreateFromAsset(GetAssets (), "font/MgOpenCosmeticaBold. ttf ");

Custom. setTypeface (typeface );

Custom. setText ("Custom Font FYI ");

Custom. setTextSize (20366f );

// Set the font color

Custom. setTextColor (Color.CYAN);

LinearLayout. addView (custom,NewLinearLayout. LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


Effect:

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.