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字体文件放到这。
Android应用使用自定义字体_第1张图片
  • 第二步,程序中调用:
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即可。
  • 对于在线的网页,则需要把字体文件放到服务器,使用同样的方式定义字体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);            CLog.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;        }

参考资料:

Android应用使用自定义字体的一些探究:http://blog.isming.me/2015/07/07/android-custom-font/

Android如何高效率的替换整个APP的字体?:http://blog.shemeng.cn/2016/04/20/android-how-efficient-is-the-replacement-of-the-entire-app-font/

更多相关文章

  1. android 创建文件夹失败
  2. Android从文件目录中写入和读取图片
  3. 布局文件中的笔记
  4. 实现android启动界面字体的动画效果
  5. 【Android】配置文件属性说明
  6. 在sd卡存储文件

随机推荐

  1. Android(安卓)library projects cannot b
  2. android俄罗斯方块完整代码
  3. Android(安卓)Studio 编译缓存(Build Cac
  4. 技能积累
  5. 安装android开发环境原始版(windows版)
  6. ViewModels 和 LiveData:模式 +反模式
  7. Mac编译FFmpeg Android动态so库实践
  8. Android之Banner的滚动轮播实现
  9. android 数据存储——SharedPreferences,
  10. Android(安卓)Design Support Library 控