介绍

最近在看开源项目的时候,发现里面涉及到了自定义字体,虽然自己目前还用不到,但是动手demo笔记记录一下还是有必要的,没准哪天需要到这个功能。

原理

1、其实实现起来非常简单,主要是用到了Typeface这个类,通过加载assets里的ttf字体,调用View.setTypeface实现原生字体替换。

默认自带样式
public static final int NORMAL = 0;
public static final int BOLD = 1;
public static final int ITALIC = 2;
public static final int BOLD_ITALIC = 3;

默认自带Typeface

public static final Typeface DEFAULT;
public static final Typeface DEFAULT_BOLD;
public static final Typeface SANS_SERIF;
public static final Typeface SERIF;
public static final Typeface MONOSPACE;

2、通过声明Paint来设置相关参数,调用View.getPaint().set***可以实现自定义样式。

3、具体相关操作可以看文档。

实现

main布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.customfont.MainActivity" > <LinearLayout
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!注意我的字体哟!"
android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="看我自定义字体" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改变字体按钮" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是原生的字体"
android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是原生的字体改样式"
android:textAppearance="?android:attr/textAppearanceLarge" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原生按钮" /> </LinearLayout> </LinearLayout>

核心代码

package com.example.customfont;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends ActionBarActivity {
TextView textView1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
textView1.getPaint().setFakeBoldText(true); //true为粗体,false为非粗体
textView1.getPaint().setTextSkewX(-0.5f); //float类型参数,负数表示右斜,正数左斜
textView1.getPaint().setUnderlineText(true); //true为下划线,false为非下划线
textView1.getPaint().setStrikeThruText(true); //true为删除线,false为非删除线 //从Assets获取字体文件
Typeface mFont = Typeface.createFromAsset(getAssets(),
"STXINGKA.TTF");
//设置字体控件
ViewGroup root = (ViewGroup) findViewById(R.id.ll_root);
setFont(root, mFont); } /**
* 设置ViewGroup中的指定的控件字体为指定字体。
* @param group
* @param font
*/
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
//判断View的类型,只设置指定类型的字体。
if (v instanceof TextView || v instanceof EditText
|| v instanceof Button) {
((TextView) v).getPaint().setTypeface(font);
}
//如果是ViewGroup,迭代。
else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
}

运行之后看起来是这样的

demo地址:

链接:http://pan.baidu.com/s/1eQoyQPW 密码:4njm

Android实现自定义字体的更多相关文章

  1. 转--Android中自定义字体的实现方法

    1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...

  2. Android 使用自定义字体

    整个项目要使用第三方字体首先将字体文件放到assets文件夹下 因为整个项目要用第三方字体这里我重写了 TextView Button EditText 三个控件 以TextView 为例代码如下   ...

  3. Android Studio 自定义字体显示英文音标

    android:fontFamily="serif" 网上查了很多自定义字体的方式,或多或少都有些麻烦,最后还是尝试着认为内置字体不应该实现不了英文音标问题,就一个一个字体试了一下 ...

  4. Typeface-为自定义字体提供字体内存缓存

    Android 上自定义字体的代码一般如下: TextView textview = (TextView) findViewById(R.id.your_referenced_textview); / ...

  5. Android SearchView 自定义SearchIcon和字体颜色大小

    自定义SearchView的搜索图标和字体属性相对复杂一些,记下来. 一.自定义SearchIcon 1.API版本低于21:版本小于21时,要修改SearchIcon比较复杂,需要先获取到Searc ...

  6. Android怎么使用字体图标 自定义FontTextView字体图标控件-- 使用方法

    首先我想说明一下字体图标的好处,最大的好处就是自适应了,而且是使用TextView 不用去切图,是矢量图 灵活调用 第一步我要说明一下一般字体图标的来源,我这里使用的是  --阿里巴巴矢量图标库 -网 ...

  7. Android 中使用自定义字体的方法

    1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . <?xml version="1.0 ...

  8. 【Android 界面效果42】如何自定义字体

    项目里要统一用设计师的字体,android:typeface只支持系统三种字体.有什么比较好的做法? 你需要为整个应用替换自定义字体. 解决方案 1)Android默认方法 #1 你可以通过ID查找到 ...

  9. Android:更好的自定义字体方案

    http://ryanhoo.github.io/blog/2014/05/05/android-better-way-to-apply-custom-font/ 情景 解决方案 1)Android默 ...

随机推荐

  1. JavaScript之四种继承方式讲解

    在Javascript中,所有开发者定义的类都可以作为基类,但出于安全性考虑,本地类和宿主类不能作为基类,这样可以防止公用访问编译过的浏览器级的代码,因为这些代码可以被用于恶意攻击. 选定基类后,就可 ...

  2. .NET设计模式(16):模版方法(Template Method)(转)

    摘要:Template Method模式是比较简单的设计模式之一,但它却是代码复用的一项基本的技术,在类库中尤其重要. 主要内容 1.概述 2.Template Method解说 3..NET中的Te ...

  3. Spring声明式事务配置管理方法(转)

    项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add libra ...

  4. xxx.properties获取方法

    1.手动获取加载 Locale locale = Locale.getDefault(); ResourceBundle localResource = ResourceBundle.getBundl ...

  5. Unity3d之音效播放和调用手机震动

    http://blog.csdn.net/sunshine_1984/article/details/12943979 今天研究了下Unity3d音效播放相关内容,整理下实现细节. 1,添加音效文件到 ...

  6. Sqli-labs less 37

    Less-37 本关与34关是大致相似的,区别在于处理post内容用的是mysql_real_escape_string()函数,而不是addslashes()函数,但是原理是一直的,上面我们已经分析 ...

  7. FM算法

    1.FM背景 在计算广告中,CTR预估(click-through rate)是非常重要的一个环节,因为DSP后面的出价要依赖于CTR预估的结果.在前面的相关博文中,我们已经提到了CTR中相关特征工程 ...

  8. light oj 1140 - How Many Zeroes? 数位DP

    思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...

  9. node中的模块

    模块 编写稍大一点的程序时一般都会将代码模块化.在NodeJS中,一般将代码合理拆分到不同的JS文件中,每一个文件就是一个模块,而文件路径就是模块名. 在编写每个模块时,都有require.expor ...

  10. js 异步请求封装

    1. function ajax(url, onsuccess) { var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ...