android全局替换字体最新方案
来源:互联网 发布:手机桌面特效软件 编辑:程序博客网 时间:2024/05/29 01:52
github上有一个Calligraphy库,这个库可以用来替换字体。核心代码就是CalligraphyLayoutInflater,它继承默认LayoutInflater并且取代默认inflater来渲染整个图层。当你设置好了字体以后自然就会用新字体来渲染文字。但问题是,现在这个库只能实现一开始加载的时候渲染文字,那如果我正在一个Activity里面操作,里面有viewpager,fragment等一大堆组件,然后我切换进设置,在设置里面修改字体再返回这个Activity,此时Activity并没有经历销毁重新生成的过程,所以新字体无法自动应用上去,那怎么办呢?我们可以这么办
(1)配置Calligraphy库
这一步可以解决在新生成的,或者销毁再生成的Activity里面的字体的变化。剩下的就是已经存在的Activity的组件,包括ListView等有缓存机制的
参考:http://www.jianshu.com/p/5d4e6ae8ba4e
(2)遍历修改
对于原生,自定义控件采用遍历修改的方法(还剩下ListView和ViewPager等有缓存机制的组件)
/** * 遍历修改字体 * @param root 根目录,一般就是最顶层的LinearLayout */ private void Traversal(ViewGroup root) { for (int i = 0; i < root.getChildCount(); i++) { View view = root.getChildAt(i); if (view instanceof ViewGroup) { Traversal((ViewGroup) view); } else { String componentName = view.getClass().toString(); Log.e("name", componentName); checkForChange(view); } } } private void checkForChange(View view) { if (view instanceof TextView) { ((TextView) view).setTypeface(mTypeface); } if (view instanceof Button) { ((Button) view).setTypeface(mTypeface); } if (view instanceof EditText) { ((EditText) view).setTypeface(mTypeface); } if (view instanceof AutoCompleteTextView) { ((AutoCompleteTextView) view).setTypeface(mTypeface); } if (view instanceof MultiAutoCompleteTextView) { ((MultiAutoCompleteTextView) view).setTypeface(mTypeface); } if (view instanceof CheckBox) { ((CheckBox) view).setTypeface(mTypeface); } if (view instanceof RadioButton) { ((RadioButton) view).setTypeface(mTypeface); } if (view instanceof ToggleButton) { ((ToggleButton) view).setTypeface(mTypeface); } if (view instanceof AppCompatTextView) { ((AppCompatTextView) view).setTypeface(mTypeface); } if (view instanceof AppCompatButton) { ((AppCompatButton) view).setTypeface(mTypeface); } if (view instanceof AppCompatEditText) { ((AppCompatEditText) view).setTypeface(mTypeface); } if (view instanceof AppCompatAutoCompleteTextView) { ((AppCompatAutoCompleteTextView) view).setTypeface(mTypeface); } if (view instanceof AutoCompleteTextView) { ((AutoCompleteTextView) view).setTypeface(mTypeface); } if (view instanceof AppCompatCheckBox) { ((AppCompatCheckBox) view).setTypeface(mTypeface); } if (view instanceof AppCompatRadioButton) { ((AppCompatRadioButton) view).setTypeface(mTypeface); } if (view instanceof AppCompatCheckedTextView) { ((AppCompatCheckedTextView) view).setTypeface(mTypeface); } }
(3)ListView更新字体
在getView函数中调用如下函数,然后在外部调用notifyDataSetChanged刷新
private void updateTypeFace() { if (mTypeface == null) { TypeFaceApplication application = (TypeFaceApplication) ((Activity) mContext).getApplication(); path = application.getCurrentAssetsPath(); mTypeface = Typeface.createFromAsset(mContext.getAssets(), path); } else { TypeFaceApplication application = (TypeFaceApplication) ((Activity) mContext).getApplication(); String currentPath = application.getCurrentAssetsPath(); if (!currentPath.equals(path)) { path = currentPath; mTypeface = Typeface.createFromAsset(mContext.getAssets(), currentPath); } } }
(4)ViewPager
在Fragment的setUserVisibleHint中刷新ListView,一般性的控件已经由之前的遍历Traversal完成
最后贴一下Application的代码
public class TypeFaceApplication extends Application { private String assetsPath = "fonts/songti.TTF"; @Override public void onCreate() { super.onCreate(); CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath(assetsPath) .setFontAttrId(R.attr.fontPath) .build()); } public void change(String assetsPath) { this.assetsPath = assetsPath; CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath(assetsPath) .setFontAttrId(R.attr.fontPath) .build()); } public String getCurrentAssetsPath() { return assetsPath; }}
阅读全文
0 0
- android全局替换字体最新方案
- Android 替换全局字体
- Android 5.0以上 app全局字体替换
- Android 全局替换第三方字库(自定义字体)
- 简单高效的实现Android App全局字体替换
- 简单高效的实现Android App全局字体替换
- 简单高效的实现Android App全局字体替换
- 全局替换字体,开源库更方便!!!
- 怎样“无痛”全局替换字体
- 太激动!Android修改全局字体样式,替换整个APP字体
- Android 改变全局字体
- Android 全局修改字体
- android更改全局字体
- 全局替换安卓应用字体
- Android应用字体替换
- Android 替换app字体
- android 替换默认字体
- Android Studio全局搜索替换
- CentOS7 修改root密码
- Android DVB底层Linux驱动
- 图片加载框架 --Picasso 的基本使用
- 鼠标点击图片后移动事件
- 字符串:字符串的反转
- android全局替换字体最新方案
- 数据结构思维 第十七章 排序
- Linux安装配置SVN服务器
- CryEngine最新版发布,支持Vulkan API
- 复选框的全选和反选
- 面试题(实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1))
- Linux系统学习过程中的疑问(进程)
- C3P0的详细配置说明
- js中几种实用的跨域方法原理详解