Parse the implementation method of using custom fonts in Android _android

Source: Internet
Author: User

1, the Android system defaults to support three kinds of fonts, respectively: "Sans", "serif", "monospace

2, in Android can introduce other fonts.

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >

<TableRow>

        <textview
             android:layout_marginright= "4px"
             android:text= "sans:"
            Android: Textsize= "20SP"
        </textview>
         <!--Use the default sans font;

<textview
Android:id= "@+id/sans"
android:text= "Hello,world"
Android:textsize= "20SP"
Android:typeface= "Sans" >
</TextView>
</TableRow>

<TableRow>

        <textview
             android:layout_marginright= "4px"
             android:text= "serif:"
            Android : textsize= "20sp"
        </textview>
         <!--Use the default serifs font;

<textview
Android:id= "@+id/serif"
android:text= "Hello,world"
Android:textsize= "20SP"
android:typeface= "Serif" >
</TextView>
</TableRow>

<TableRow>

        <textview
             android:layout_marginright= "4px"
             android:text= "monospace:"
            Android:textsize= "20SP"
        </textview>
         <!--Use the default monospace font;

<textview
Android:id= "@+id/monospace"
android:text= "Hello,world"
Android:textsize= "20SP"
Android:typeface= "Monospace" >
</TextView>
</TableRow>
<!--there is no font set here, we will set--> in Java code

<TableRow>

<textview
android:layout_marginright= "4PX"
android:text= "Custom:"
Android:textsize= "20SP" >
</TextView>

<textview
Android:id= "@+id/custom"
android:text= "Hello,world"
Android:textsize= "20SP" >
</TextView>
</TableRow>

</TableLayout>


Copy Code code as follows:

Get TextView Control Object
TextView TextView = (TextView) Findviewbyid (R.id.custom);
Save the font file in the assets/fonts/directory, www.linuxidc.com create the typeface object
Typeface typeface = Typeface.createfromasset (Getassets (), "Fonts/droidsansthai.ttf");
Apply Font
Textview.settypeface (typeface);

if you want to apply a custom font to all controls on the entire interface, you can:
Copy Code code as follows:

Package arui.blog.csdn.net;

Import android.app.Activity;
Import Android.graphics.Typeface;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;

public class Fontmanager {

public static void Changefonts (ViewGroup root, Activity Act) {

typeface tf = Typeface.createfromasset (Act.getassets (),
"Fonts/xxx.ttf");

for (int i = 0; i < Root.getchildcount (); i++) {
View v = root.getchildat (i);
if (v instanceof TextView) {
((TextView) v). Settypeface (TF);
else if (v instanceof Button) {
((Button) v). Settypeface (TF);
else if (v instanceof edittext) {
((EditText) v). Settypeface (TF);
else if (v instanceof viewgroup) {
Changefonts ((viewgroup) v, ACT);
}
}

}
}

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.