• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

Android开发中怎么使用自定义字体?

时间:2015年04月02日 15:31:19    来源:魔法猪系统重装大师官网    人气:11023

1、Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 

2、在Android中可以引入其他字体 。

<?xml version="1.0" encoding="utf-8"?>
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent" >

   

                    Android:layout_marginRight="4px"
            Android:text="sans:"
            Android:textSize="20sp" >
       
       

                    Android:id="@+id/sans"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="sans" >
       
   


   

                    Android:layout_marginRight="4px"
            Android:text="serif:"
            Android:textSize="20sp" >
       
       

                    Android:id="@+id/serif"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="serif" >
       
   


   

                    Android:layout_marginRight="4px"
            Android:text="monospace:"
            Android:textSize="20sp" >
       
       

                    Android:id="@+id/monospace"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="monospace" >
       
   

   

   

                    Android:layout_marginRight="4px"
            Android:text="custom:"
            Android:textSize="20sp" >
       

                    Android:id="@+id/custom"
            Android:text="Hello,World"
            Android:textSize="20sp" >
       
   


// 得到TextView控件对象
TextView textView = (TextView) findViewById(R.id.custom);
// 将字体文件保存在assets/fonts/目录下,www.linuxidc.com创建Typeface对象
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/DroidSansThai.ttf");
// 应用字体
textView.setTypeface(typeFace);

如果想对整个界面的所有控件都应用自定义字体,可以:

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);  

           }  

       }  

    }  

}  

Android,开发,中,怎么,使用,自定义,字体,、,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部