Android开发--更换字体
来源:互联网 发布:java虚拟机占用内存 编辑:程序博客网 时间:2024/06/07 01:58
开发中,经常遇到需要更换字体格式,设计妹子为了让UI更美,设置了很多让人着(tong)迷(ku)的字体,但是蓝瘦归蓝瘦,还是得撸起袖子开干。
Android系统中,默认提供三种字体:"sans", "serif", "monospace"
如果设置字体为系统字体之一,在XML中,直接设置字体格式:
1、sans<TextView Android:id="@+id/sans" Android:text="sans" Android:textSize="10sp" Android:typeface="sans" />
2、monospace
<TextView Android:id="@+id/monospace" Android:text="monospace" Android:textSize="10sp" Android:typeface="monospace" />
3、serif
<TextView Android:id="@+id/serif" Android:text="serif" Android:textSize="10sp" Android:typeface="serif" />
使用自定义字体
首先要引入自定义的字体文件,例如引入Roboto-Light字体,将Roboto-Light.ttf放入assets\fonts\目录下
1、一般情况下,只针对部分字体,或者某个界面的少量字体进行修改
在XML布局中,不做任何修改:
<TextView Android:id="@+id/textview " Android:text="custom" Android:textSize="10sp" />
在代码中:
得到TextView对象
TextView textView =(TextView)findViewById(R.id.textview);
创建Typeface对象
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Roboto-Light.ttf");
设置字体
textView.setTypeface(typeFace);
简单的三步,搞定。
2、上面是针对部分字体修改,但有时设计为了App的整体美观或者App产品本身的定位,绝大部分甚至所有字体,都需要使用自定义的字体。
如果再使用上面的方法逐一修改,对开发者来说无异于噩耗,而且,这种毫无意义的重复劳动,也不符合我们能懒就懒得程序员风格
那就想点省事的方法吧:
(1)、获取系统字体,并替换
public final class FontsOverride { public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) { final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName); replaceFont(staticTypefaceFieldName, regular); } protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) { try { final Field staticField = Typeface.class .getDeclaredField(staticTypefaceFieldName); staticField.setAccessible(true); staticField.set(null, newTypeface); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }}
在Application类中替换系统默认字体
public final class App extends Application { @Override public void onCreate() { super.onCreate(); FontsOverride.setDefaultFont(this, "DEFAULT", "Roboto-Light.ttf"); }}
(2)、上面是全局替换,但更多时候开发中只需要替换一部分,甚至有时想在XML中设置字体:
自定义CustomTextView对象
public class CustomTextView extends TextView {public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init();}public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init();}public CustomTextView(Context context) { super(context); init();}private void init() { if (!isInEditMode()) { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf"); setTypeface(tf); }}
XML中使用自定义的CustomTextView
<com.packagename.CustomTextView android:id="@+id/custom" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Custom TextView" android:textappearance="?android:attr/textAppearanceLarge"/>
总结:如果使用Android系统提供字体,直接在XML布局中设置TextView的typeface;
如果使用自定义字体,则根据具体需求,选择对应的方法
阅读全文
0 0
- Android开发--更换字体
- Android开发--更换字体
- Android APP开发字体更换
- android 更换字体
- Android TextView更换字体
- Android 如何更换系统字体
- android-settings添加字体更换-中篇-FontStyle更换的解决方案
- 快速使用反射更换Android全局字体
- Android开发 support V4 更换
- android-settings添加字体更换-前篇-setting简单一览
- 一种简单的Android 中全局更换字体的方法
- 关于Android如何更换整个应用字体的总结
- android开发教程:Android更换RatingBar图片
- Android开发:自定义字体
- 更换桌面图标字体
- 关于更换手机字体
- 更换Linux下字体
- cm12 系统字体更换
- Android获取设备唯一标识
- - Unable to add window -- token null is not valid; is your activity running?
- Error:Execution failed for task ':app:transformNative_libsWithStripDebugSymbolForDebug'. > java.lang.NullPointerExcep
- win下的问题积累
- Android开发中--正则表达式的实用
- Android开发--更换字体
- KnockoutJS(一)
- Android开发—使用系统控件获取当前时间
- 如何从零开始搭建自己的博客(通俗易懂)
- Androidstudio使用Git从远端仓库Clone分支到本地
- AndroidStudio 利用Gradle打包添加上版本名和版本号
- 日常错误集合
- Androidstudio高效管理第三方API的KEY及Gradle版本管理
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁、旋转、翻转照片