解析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);  
           }  
       }

}  
}

(0)

相关推荐

  • Android 中SP与DP的区别实例详解

    从一开始写Android程序,就被告知这些常识 1.长度宽度的数值要使用dp作为单位放入dimens.xml文件中 2.字体大小的数值要使用sp作为单位,也放入dimens.xml文件中 然后,就没有然后了,仿佛潜台词就是说,你记住去用就行了. 偶然有一天,当我们阴差阳错地将字体写成了dp,也是可以工作,而且效果和sp一样. 这时候,就开始怀疑了,到底有啥区别呢,dp和sp有什么不同呢? 我们做个简单的Sample验证一下,如下,一个布局代码 <TextView android:layout_w

  • Android实现360手机助手底部的动画菜单

    首先来看下我们实现的效果和360效果的对比: 360手机助手效果演示 本库实现的效果(Icon来自360手机助手,侵删) xml布局文件 注:为了美观,讲每个Button的高度以及固定,设置wrap_content时候是最大高度,为50dp,如果需要设置特定高度请参见下文的方法表格 <com.brioal.bottomtab.view.BottomLayout android:id="@+id/main_tab" android:layout_width="match_

  • Android自定义控件实现随手指移动的小球

    一个关于自定义控件的小Demo,随着手指移动的小球. 先看下效果图: 实现代码如下: 1.自定义控件类 package com.dc.customview.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import

  • 手把手教你用Android自定义饼状图

    照例先上效果图 通过该例子,你能学到什么: 对Paint 深入理解,画绘制饼图,矩形,文字等 加深对canvas的API的掌握,对自定义View掌握 下面我们分七步来完成一个简单的饼形图绘制过程. 1. 重新View的构造方法 public PieView(Context context) { this(context, null); } public PieView(Context context, AttributeSet attrs) { this(context, attrs, 0);

  • Android仿新浪微博、QQ空间等帖子显示(1)

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,实际应用中用的比较多的地方比如聊天时显示表情啊,朋友圈或社区中话题的显示.@好友显示和点击等等,关键字显示不同颜色-- 1.BackgroundColorSpan 背景色 SpannableString spanText = new SpannableString("BackgroundColorSpan"); sp

  • Android使用自定义字体的方法

    本文实例讲述了Android使用自定义字体的方法.分享给大家供大家参考,具体如下: 一.问题: 作为android初学者,在做一个游戏,游戏的标题我用的是TextView,android只提供斜体粗体,但把字体设置成楷体游戏的界面会更好,怎么才能实现. 二.解决方法: 使用自定义字体 android Typeface使用TTF字体文件设置字体 我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体. 第一步,在assets目录下新建fonts目录,把ttf字体文件放到这. 第二

  • Android Back键点击两次退出应用详解及实现方法总结

    思路:Android中捕获用户按键是在onKeyDown方法中,只需要判断用户按键是否是KEYCODE_BACK即后退键即可,剩下的即为判断两次点击BACK键时间间隔问题了 第一种实现方式 package com.example.clickexittest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import andr

  • Android APP使用自定义字体实现方法

    android系统内置字体 android 系统本身内置了一些字体,可以在程序中使用,并且支持在xml配置textView的时候进行修改字体的样式.支持字段为android:textStyle ,android:typeface, android:fontFamily,系统内置了normal|bold|italic三种style, 内置了normal,sans,serif,monospace,几种字体(实测这几种字体仅英文有效),typace和fontFamily功能一样. 使用自定义的字体 以

  • Android获取手机通话记录的方法

    Android如何获取手机通话记录,本文为大家揭晓. 获取手机通话记录流程: 1. 获取ContentResolver; ContentResolver resolver = getContentResolver(); 2.resolver.query(*); 需要传入通话记录的URI:CallLog.Calls.CONTENT_URI 3.对查询得到的Cursor进行数据获取. 主要代码如下: MainActivity.java package com.noonecode.contentres

  • Android仿新浪微博、QQ空间等帖子显示(2)

    一.介绍 这是新浪微博的一个帖子,刚好包括了话题.表情.@好友三种显示.显示方法上篇已经阐述了,就是使用SpannableString.这篇主要介绍显示这种帖子的解析工具类. 二.实现 1.字符串表示和对应正则表达式 话题用##号括起来 表情用[]表示 @好友昵称 借助正则匹配来解析帖子信息. 话题 -> #[^#]+# 表情 -> [[^]]+] @好友 -> @好友昵称 2.写一个通用方法,对spanableString进行正则判断,如果符合要求,则将内容变色 private sta

  • Android Gradle Build Error:Some file crunching failed, see logs for details的快速解决方法

    错误日志:Error:java.lang.RuntimeException: Some file crunching failed, see logs for details Log: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:mergeDebugResources'. > Error: java.lang.RuntimeException: Crunch

随机推荐