当前位置: 移动技术网 > IT编程>移动开发>Android > Android APP使用自定义字体实现方法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

女孩要什么,youtube翻墙软件,寒假里难忘的一件事

android系统内置字体

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

使用自定义的字体

以上的方式可以改变字体的样式,还不是真正的自定义。android系统支持typeface,即ttf的字体文件。我们可以在程序中放入ttf字体文件,在程序中使用typeface设置字体。

第一步,在assets目录下新建fonts目录,把ttf字体文件放到这。

第二步,程序中调用:

public class mainactivity extends appcompatactivity {
   private textview textview;
   @override
   protected void oncreate(bundle savedinstancestate) {
     super.oncreate(savedinstancestate);
     setcontentview(r.layout.activity_main);
     textview= (textview) findviewbyid(r.id.text);
     assetmanager assets = getassets();
     typeface fromasset = typeface.createfromasset(assets, "fonts/fzlt.ttf");
     textview.settypeface(fromasset);
  }
}

注意ttf文件命名不能使用中文,否则可能无法加载。

对于需要使用比较多的地方,可以写一个textview的子类来统一处理。

public class customtextview extends textview {
 
   public customtextview(context context) {
     super(context);
     // todo auto-generated constructor stub
   }
 
   public customtextview(context context, attributeset attrs) {
     super(context,attrs);
     // todo auto-generated constructor stub
   }
 
   public customtextview(context context, attributeset attrs,int defstyle) {
     super(context,attrs,defstyle);
     // todo auto-generated constructor stub
   }
 
   public void settypeface(typeface tf, int style) {
     super.settypeface(appcontext.getinstance().gettypeface());
   }
 
}

//初始化自定义字体
typeface = typeface.createfromasset(getassets(), "fonts/fzlt.ttf");

法还是有点缺点的:只能替换一类控件的字体,如果需要替换button或edittext控件的字体,需要以相同的方式自定义这些控件,这样工作量大,如何高效替换整个app中的字体,见下方参考资料。

在webview中使用自定义的字体

对于本地的网页,在asset目录放字体文件,并在css中添加以下内容,自定义一个字体face,并且在需要的地方使用这个字体face即可。

<style>
@font-face {
   font-family: 'myface';
   src: url('file:///android_asset/fonts/fzlt.ttf');
}
body {
   margin: 0;
   padding: 0;
   font-family:'myface','方正兰亭纤黑简体';
}
.textbar{ box-sizing:border-box; width:100%; padding:5px;}
.textbar p{ font-size:16px; text-align:justify; color:#333;line-height:24px; margin:0 0 0 0;}
.textbar h1{ font-size:18px; margin:10px 0 10px 0;color:#000}
</style>

对于在线的网页,则需要把字体文件放到服务器,使用同样的方式定义字体face,应用到每个地方。
为了减少网页或者说服务器端的工作,可以使用本地注入的方式注入font-face的css,并对整个网页进行样式替换。给webview自定义webviewclient,重写onpagefinish,在其中添加如下内容:

view.loadurl("javascript:!function(){" + "s=document.createelement('style');s.innerhtml=" + "\"@font-face{font-family:myhyqh;src:url('**injection**/hyqh.ttf');}*{font-family:myhyqh !important;}\";"
+ "document.getelementsbytagname('head')[0].appendchild(s);" +
"document.getelementsbytagname('body')[0].style.fontfamily = \"myhyqh\";}()");
 
//由于网页上是没有权限访问本地的asset文件夹的,因此我们需要拦截请求来加载本地的文件,我这里替换了`file:
//android_assets/`为 `**injection**/`了,我们还需要重写`shouldinterceptrequest`
//在请求为我们这个字体文件的时候,加载本地文件:
@override
public webresourceresponse shouldinterceptrequest (webview view, string url){
   webresourceresponse response = super.shouldinterceptrequest(view, url);
   log.i("load intercept request:" + url);
   if (url != null && url.contains("**injection**/")) {
     //string assertpath = url.replace("**injection**/", "");
     string assertpath = url.substring(url.indexof("**injection**/") + "**injection**/".length(), url.length());
     try {
        response = new webresourceresponse("application/x-font-ttf", "utf8", getassets().open(assertpath));
     } catch (ioexception e) {
        e.printstacktrace();
     }
   }
   return response;
}



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网