Android修改字体样式的示例代码

在Android实际开发中根据UI的设计图,经常要去改变系统默认的字体样式

这样做会使apk变大很多啊

而且为什么android要使用ios的字体-_-#

单独设置字体样式

(1)Android系统提供了几种字体样式可供选择

通过设置typeface属性或者fontFamily属性设置

typeface属性:

normal serif sans monospace

fontFamily属性:

casual cursive serif monospace sans-serif sans-serif-condensed serif-monospace sans-serif-smallcaps

※typeface和fontFamily区别

android:typeface属性是增加API1

android:fontFamily在API16(4.1)中添加了属性

※当同时设置typeface和fontFamily时,只有fontFamily生效

查看一波TextView的源码

private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) { Typeface tf = null; if (familyName != null) { tf = Typeface.create(familyName, styleIndex); if (tf != null) { setTypeface(tf); return; } } switch (typefaceIndex) { case SANS: tf = Typeface.SANS_SERIF; break; case SERIF: tf = Typeface.SERIF; break; case MONOSPACE: tf = Typeface.MONOSPACE; break; } setTypeface(tf, styleIndex); }

从方法setTypefaceFromAttrs()看,如果你有set fontFamily属性,那么typefaceattribute将被忽略。

这边会发现这样设置typeface和fontFamily属性对中文不生效,这时候就需要引用外部的字体样式(这里谷歌设计规范推荐使用NOTO字体https://www.google.com/get/noto/)

(2)使用字体样式文件设置(otf,ttf文件都可以)

在assets下新建一个fonts文件,把字体样式文件放进去

在代码中设置

AssetManager mgr = getAssets(); Typeface tf = Typeface.createFromAsset(mgr, "fonts/NotoSansCJKsc-Black.otf"); tv_1.setTypeface(tf);

批量设置字体样式

(1)自定义TextView

public class CustomTextView extends TextView { public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } //重写设置字体方法 @Override public void setTypeface(Typeface tf) { tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/NotoSansCJKsc-Light.otf"); super.setTypeface(tf); } }

之后在XML布局文件中使用CustomTextView代替TextView

<com.test.fontfamily.CustomTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dp" android:text="自定义字体" android:textSize="24dp" />

(2)更换整个App的字体

思路:遍历找到所有的TextView然后替换字体

百度了一下找到下面工具类

package com.test.fontfamily; import android.app.Application; import android.content.Context; import android.graphics.Typeface; import android.support.annotation.NonNull; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.lang.ref.SoftReference; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; /** * Created by Administrator on 2017/10/24. */ public class FontUtils { private static final String TAG = FontUtils.class.getSimpleName(); private Map<String, SoftReference<Typeface>> mCache = new HashMap<>(); private static FontUtils sSingleton = null; public static Typeface DEFAULT = Typeface.DEFAULT; // disable instantiate private FontUtils() { } public static FontUtils getInstance() { // double check if (sSingleton == null) { synchronized (FontUtils.class) { if (sSingleton == null) { sSingleton = new FontUtils(); } } } return sSingleton; } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath font file path relative to 'assets' directory. */ public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath) { replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath)); } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath font file path relative to 'assets' directory. * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style) { replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style); } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath The full path to the font data. */ public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath) { replaceFont(root, createTypefaceFromFile(fontPath)); } /** * <p>Replace the font of specified view and it's children</p> * * @param root The root view. * @param fontPath The full path to the font data. * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style) { replaceFont(root, createTypefaceFromFile(fontPath), style); } /** * <p>Replace the font of specified view and it's children with specified typeface</p> */ private void replaceFont(@NonNull View root, @NonNull Typeface typeface) { if (root == null || typeface == null) { return; } if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font TextView textView = (TextView) root; // Extract previous style of TextView int style = Typeface.NORMAL; if (textView.getTypeface() != null) { style = textView.getTypeface().getStyle(); } textView.setTypeface(typeface, style); } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount(); ++i) { replaceFont(viewGroup.getChildAt(i), typeface); } } // else return } /** * <p>Replace the font of specified view and it's children with specified typeface and text style</p> * * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style) { if (root == null || typeface == null) { return; } if (style < 0 || style > 3) { style = Typeface.NORMAL; } if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font TextView textView = (TextView) root; textView.setTypeface(typeface, style); } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount(); ++i) { replaceFont(viewGroup.getChildAt(i), typeface, style); } } // else return } /** * <p>Create a Typeface instance with specified font file</p> * * @param fontPath font file path relative to 'assets' directory. * @return Return created typeface instance. */ private Typeface createTypefaceFromAsset(Context context, String fontPath) { SoftReference<Typeface> typefaceRef = mCache.get(fontPath); Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get()) == null) { typeface = Typeface.createFromAsset(context.getAssets(), fontPath); typefaceRef = new SoftReference<>(typeface); mCache.put(fontPath, typefaceRef); } return typeface; } private Typeface createTypefaceFromFile(String fontPath) { SoftReference<Typeface> typefaceRef = mCache.get(fontPath); Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get()) == null) { typeface = Typeface.createFromFile(fontPath); typefaceRef = new SoftReference<>(typeface); mCache.put(fontPath, typefaceRef); } return typeface; } /** * <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p> * {@code <item name="android:typeface">monospace</item>} * <p>The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p> * * @param context {@link Context Context} * @param fontPath font file path relative to 'assets' directory. */ public void replaceSystemDefaultFontFromAsset(@NonNull Context context, @NonNull String fontPath) { replaceSystemDefaultFont(createTypefaceFromAsset(context, fontPath)); } /** * <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p> * {@code <item name="android:typeface">monospace</item>} * <p>The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p> * * @param context {@link Context Context} * @param fontPath The full path to the font data. */ public void replaceSystemDefaultFontFromFile(@NonNull Context context, @NonNull String fontPath) { replaceSystemDefaultFont(createTypefaceFromFile(fontPath)); } /** * <p>Replace system default font. <b>Note:</b>you should also add code below to your app theme in styles.xml. </p> * {@code <item name="android:typeface">monospace</item>} * <p>The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.</p> */ private void replaceSystemDefaultFont(@NonNull Typeface typeface) { modifyObjectField(null, "MONOSPACE", typeface); } private void modifyObjectField(Object obj, String fieldName, Object value) { try { Field defaultField = Typeface.class.getDeclaredField(fieldName); defaultField.setAccessible(true); defaultField.set(obj, value); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }

阅读源码发现这里面主要方法有

replaceFont()

替换一个页面中的所有字体。用递归的方式去查找view是否是TextView或者TextView的子类,然后进行替换。不过这种方法效率不高

用法:在页面中

FontUtils.getInstance().replaceFontFromAsset(View root, String fontPath)

modifyObjectField()

替换App所有字体。利用反射替换系统默认字体

用法:

a.新建一个BaseApplication继承Application在onCreate方法中

FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this,"fonts/NotoSansCJKsc-Thin.otf");

b.在AppTheme中添加

<item name="android:typeface">monospace</item>

c.清单文件中使用BaseApplication

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-07-28 16:56:39

Android修改字体样式的示例代码的相关文章

JS获取和修改元素样式的实例代码_javascript技巧

1.获取元素样式: 可以通过元素的style属性,获取元素行内样式.style属性是一个对象,包括一系列样式属性.例如:color, backgourdColor. 上面讲的通过style属性获取元素样式,不推荐使用. 下面的一段代码,可以获取元素运行时的样式,即全局的样式.这种方式可以动态获取元素的样式,例如元素大小. // node:将要获取其计算样式的元素节点 // attr: 样式属性名称 function getCurrentStyle(node, attr) { var style

Android Menu详解及示例代码_Android

Android Menu 详细介绍: 1.选项菜单 OptionsMenu 2.上下文菜单 ContextMenu 3.子菜单 SubMenu 组成Android用户界面的除了View以外,还有菜单和对话框,这一讲我们就共同学习一下菜单的使用. 菜单是用户界面中最常见的元素,使用也非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu),下面分别举例说明. 一.选项菜单 OptionsMenu Andro

Android GPS详解及示例代码_Android

LBS(Location Based Services)直译的话就是基于地理位置的服务,这里面至少有两层意思,第一要能轻易的获取当前的地理位置,譬如经纬度海拔等,另一个就是在当前位置的基础上提供增值服务,譬如找附近的加油站.餐馆.酒店等.这里面的第一步:获取用户当前位置,我们就可以用Android的GPS定位服务来得到.Android提供了基于网络的定位服务和基于卫星的定位服务两种.在设置->位置和安全设置里面的前三项就是,最后一个增强型GPS是为了辅助快速找卫星的.  那么我们现在就写一个简单

Android Service详解及示例代码_Android

Android Service 详细介绍: 1.Service的概念 2.Service的生命周期 3.实例:控制音乐播放的Service 一.Service的概念 Service是Android程序中四大基础组件之一,它和Activity一样都是Context的子类,只不过它没有UI界面,是在后台运行的组件. 二.Service的生命周期 Service对象不能自己启动,需要通过某个Activity.Service或者其他Context对象来启动.启动的方法有两种,Context.startS

Android SQLite详解及示例代码_Android

在Android中使用SQLite数据库的入门指南,打算分下面几部分与大家一起分享, 1.什么是SQLite 2.Android中使用SQLite 一.什么是SQLite SQLite是一款开源的.轻量级的.嵌入式的.关系型数据库.它在2000年由D. Richard Hipp发布,可以支援Java.Net.PHP.Ruby.Python.Perl.C等几乎所有的现代编程语言,支持Windows.Linux.Unix.Mac OS.Android.IOS等几乎所有的主流操作系统平台. SQLit

Android EditText详解及示例代码_Android

   EditText在API中的结构 java.lang.Object android.view.View android.widget.TextView android.widget.EditText 已知直接子类: AutoCompleteTextView, ExtractEditText 已知间接子类: MultiAutoCompleteTextView EditText是TextView的直接子类 所以EditText会继承父类TextView的一些方法.下面我用自己写的一个Demo

Android的HTTP多线程下载示例代码

本示例介绍在Android平台下通过HTTP协议实现断点续传下载. 多线程断点需要的功能 1.多线程下载, 2.支持断点. 使用多线程的好处:使用多线程下载会提升文件下载的速度. 多线程下载文件的过程是: (1)首先获得下载文件的长度,然后设置本地文件的长度. HttpURLConnection.getContentLength();//获取下载文件的长度 RandomAccessFile file = new RandomAccessFile("QQWubiSetup.exe",&q

Android GPS详解及示例代码

LBS(Location Based Services)直译的话就是基于地理位置的服务,这里面至少有两层意思,第一要能轻易的获取当前的地理位置,譬如经纬度海拔等,另一个就是在当前位置的基础上提供增值服务,譬如找附近的加油站.餐馆.酒店等.这里面的第一步:获取用户当前位置,我们就可以用Android的GPS定位服务来得到.Android提供了基于网络的定位服务和基于卫星的定位服务两种.在设置->位置和安全设置里面的前三项就是,最后一个增强型GPS是为了辅助快速找卫星的. 那么我们现在就写一个简单的

Android Java实现余弦匹配算法示例代码_Android

 Java实现余弦匹配算法 最近在做一个通讯交友的项目,项目中有一个这样的需求,通过用户的兴趣爱好,为用户寻找推荐兴趣相近的好友.其实思路好简单,把用户的兴趣爱好和其他用户的兴趣爱好进行一个匹配,当他们的爱好相似度比较高的时候就给双方进行推荐.那么如何进行比较是一个问题,其实我们可以通过余弦匹配算法来对用户的兴趣爱好进行比较,根据计算出来的值来得到一个兴趣爱好相近好友列表,并进行排序. 因为我做的项目是Android端的,所以算法是通过Java实现的,废话不过多说了,下面是算法的实现: pack