欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-28 21:19:52
在android实际开发中根据ui的设计图,经常要去改变系统默认的字体样式 这样做会使apk变大很多啊 而且为什么android要使用ios的字体-_-# 单...

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

这样做会使apk变大很多啊

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

单独设置字体样式

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

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

typeface属性:

  1. normal
  2. serif
  3. sans
  4. monospace

fontfamily属性:

  1. casual
  2. cursive
  3. serif
  4. monospace
  5. sans-serif
  6. sans-serif-condensed
  7. serif-monospace
  8. sans-serif-smallcaps

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

※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字体)

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

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

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

在代码中设置

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

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

(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

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