解析Android中使用自定义字体的实现方法

 更新时间:2013年05月10日 09:24:39   作者:  
本篇文章是对在Android中使用自定义字体的方法进行了详细的分析介绍。需要的朋友参考下

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

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

复制代码 代码如下:

<?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>
        <!-- 使用默认的sans字体 -->

        <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>
        <!-- 使用默认的serifs字体 -->

        <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>
        <!-- 使用默认的monospace字体 -->

        <TextView
            Android:id="@+id/monospace"
            Android:text="Hello,World"
            Android:textSize="20sp"
            Android:typeface="monospace" >
        </TextView>
    </TableRow>
    <!-- 这里没有设定字体,我们将在Java代码中设定 -->

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


复制代码 代码如下:

// 得到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);  
           }  
       }  

    }  

相关文章

  • Kotlin中?和!!的区别详细对比

    Kotlin中?和!!的区别详细对比

    这篇文章主要给大家介绍了关于Kotlin中?和!!区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • android编程之ip2id程序实例

    android编程之ip2id程序实例

    这篇文章主要介绍了android编程之ip2id程序,实例分析了Android实现ip转换id的相关技巧,需要的朋友可以参考下
    2015-04-04
  • Android 得到连接热点的ip的方法

    Android 得到连接热点的ip的方法

    这篇文章主要介绍了Android 得到连接热点的ip的方法 ,需要的朋友可以参考下
    2018-01-01
  • Android实现从底部弹出的Dialog的实例代码

    Android实现从底部弹出的Dialog的实例代码

    这篇文章主要介绍了Android实现从底部弹出的Dialog的实例代码,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
    2018-04-04
  • Android  AbsoluteLayout和RelativeLayout布局详解

    Android AbsoluteLayout和RelativeLayout布局详解

    本文主要讲解Android AbsoluteLayout和RelativeLayout布局,这里整理了相关资料,并附示例代码和效果图,有兴趣的小伙伴可以参考下
    2016-08-08
  • 使用IntelliJ IDEA 配置安卓(Android)开发环境的教程详解(新手必看)

    使用IntelliJ IDEA 配置安卓(Android)开发环境的教程详解(新手必看)

    这篇文章主要介绍了使用IntelliJ IDEA 配置安卓(Android)开发环境的教程详解(新手必看),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 浅谈Okhttp去除请求头user-agent

    浅谈Okhttp去除请求头user-agent

    本篇文章主要介绍了浅谈Okhttp去除请求头user-agent,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • SimpleCommand实现图片下载(二)

    SimpleCommand实现图片下载(二)

    这篇文章主要为大家详细介绍了SimpleCommand实现图片下载,并显示到ImageView控件上,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • android车牌识别系统EasyPR使用详解

    android车牌识别系统EasyPR使用详解

    这篇文章主要为大家详细介绍了android车牌识别系统EasyPR使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android振动器使用方法详解

    Android振动器使用方法详解

    这篇文章主要为大家详细介绍了Android振动器使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论