android app字体库,Android 自定义APP字体

一、设置全局字体

方法1:使用反射机制进行设置

(1)把系统的typeface替换为自定义的

Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/aa.ttf");

try {

// xml属性值与Typeface属性值对应

// normal Typeface.DEFAULT

// sans Typeface.SANS_SERIF

// serif Typeface.SERIF

// monospace Typeface.MONOSPACE

Field field = Typeface.class.getDeclaredField("SERIF" );

field.setAccessible( true);

field.set( null, typeFace );

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

(2)设置typeface值为 sans、 serif、 monospace其中一种,可在TextView、Activity、Application里面的xml样式里面设置,例如

android:id="@+id/tv_typeface"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:typeface="serif"

android:text="Hellow world ~" />

二、 设置局部字体

在main下新建【assets/fonts】目录,把【.ttf或者.otf】文件放进去。然后使用如下:

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/aa.ttf");

textView.setTypeface(typeface);

三、设置WebView页面的字体

让开发的HTML5页面的小伙伴在设置页面的字体的时候引用APP中main/assets/fonts目录下的字体文件就可以了,在哪个模块使用改字体就在哪个位置用HTML5标签包起来,才能设置某一块文字的字体。如果要设置全局WebView的字体的话,就在HTML根标签上设置就好了,这样整篇文章就能使用该字体文件。

四、设置系统字体

TextView tvTypeface = (TextView) findViewById(R.id.tv_typeface);

tvTypeface.setTypeface(Typeface.create("sans-serif-condensed",Typeface.NORMAL));

五、设置全局字体

方法1:新建【res/font】,把【.ttf或者.otf】文件放进去,然后在XML文件中引用,例如:

android:id="@+id/tv_typeface"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:fontFamily="@font/typeface_bold"

android:text="Hellow world ~" />

以上设置注意 : fontFamily API16 新增,所以要使用低版本的兼容库 com.android.support:appcompat-v7:26.0.0 以上

如果要在代码中设置,需要这么写:

Typeface.create(ResourcesCompat.getFont(context, R.font.typeface_bold), Typeface.NORMAL);

方法2:先设置全局主题的字体,然后再在中引用该主题或者在Activity中设置style(设置该主题的Activity才生效)

casual

或者

@font/aa

注意 :fontFamily API16 新增,所以要使用低版本的兼容库 com.android.support:appcompat-v7:26.0.0 以上,需要设置theme继承字Theme.AppCompat主题

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/FontStyle">

android:name=".CustomTypefaceActivity"

android:theme="@style/FontStyle" />

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值