Android开发之字体设置
默认字体
Android SDK自带了四种字体:"normal"“monospace",“sans”, “serif”,如下:

看这四兄弟长的还是蛮像,我是看不出多大差别。。。
设置方式
1.通过XML文件设置
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monospace"
android:textSize="20dp"
android:textColor="#000000"
android:typeface="monospace"
android:layout_margin="5dp"/>
2.Java代码中设置
TextView txtNormal = (TextView) findViewById(R.id.txt_normal);
txtNormal.setTypeface(Typeface.MONOSPACE);
设置第三方字体
Res中使用
右键选择项目的app / res文件夹,然后选择New > Android resource directory。

Resource type中选择font,File name名为font。

将字体文件拷贝到font中

java代码中使用
TextView txtNormal = (TextView) findViewById(R.id.txt_helvetica);
Typeface typeface = ResourcesCompat.getFont(this, R.font.helvetica);
txtNormal.setTypeface(typeface);
Assets中使用
新建Assets及fonts目录,并将字体文件拷贝到fonts目录下

在java代码中使用
TextView txtNormal = (TextView) findViewById(R.id.txt_helvetica);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");
txtNormal.setTypeface(typeface);
第三方框架全局字体设置
这里推荐一个第三方字体设置库Calligraphy,详细可以点击连接
添加依赖
compile 'uk.co.chrisjenx:calligraphy:2.3.0'
新建Application
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Helvetica.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
}
在Activity中重写attachBaseContext方法
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
在xml中使用
<TextView
android:id="@+id/txt_helvetica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum"
android:textSize="20dp"
android:textColor="#000000"
android:layout_margin="5dp"
fontPath="fonts/Helvetica.ttf"
tools:ignore="MissingPrefix"/>
如果fontPath="fonts/Helvetica.ttf"报错,在View上添加 tools:ignore="MissingPrefix”即可。
其他具体功能,详见Calligraphy
Android开发之字体设置的更多相关文章
- Android开发之动态设置字体的样式和粗细
字体设置通常有两种形式: 1:在xml中直接设置 android:textStyle="bold" android:typeface="sans" 2:用jav ...
- android开发之-软件设置保存-快速学会使用SharedPreferences篇-实测
我们在设计软件的时候,需要记录软件设置的基本信息,那么怎么来保存他们呢?我们可以使用SharedPreferences. SharedPreferences是一个xml文件,用来存储软件的常规设置 ...
- Android开发常用权限设置
加在AndroidManifest.xml 文件中manifest标签以内,application以外 例如:<!--网络权限 --> <uses-permission androi ...
- Android APP的字体设置
Android系统自带有对字体的设置,这些设置是对字体的显示方式的设置,比如加粗,倾斜,下划线,字号等,但是对于字体本身,比如设置为楷体,隶书等貌似没有.Android系统只有一种默认的,如果需要修改 ...
- Android SpannableString及字体设置
import android.content.Context; import android.graphics.Color; import android.text.SpannableString; ...
- Android开发——Toolbar常用设置
本篇笔记用来记录常用的Toolbar设置,如Toolbar颜色设置,显示返回按钮,显示右边三个点按钮 之前Android 使用的ActionBar,Android5.0开始,谷歌官方推荐使用Toolb ...
- android开发之当设置textview多少字后以省略号显示。限制TextView的字数
设置多少字后以省略号显示 <TextView android:id="@+id/tv" android:layout_width="wrap_conten ...
- Android(java)学习笔记163:Android开发时候颜色设置是bgr不是rgb
eb" android:background ="#"
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
== 0) { convertView.setBackgroundResource(R.drawable.list_gray_9); } else { convertView.setBackgroun ...
随机推荐
- windows下安装和使用scrapy
首先,要确保已经正确安装了python环境,并安装了pip包 接着,打开cmd或者powershell ,输入命令 pip install scrapy .安装完之后 运行scrapy性能测试命令: ...
- git 版本管控 发布
https://www.cnblogs.com/charlesblc/p/6051569.html http://www.ruanyifeng.com/blog/2012/07/git.html 1. ...
- Sql JOIN 一张图说明
一图说明:
- 其它综合-CentOS7 忘记root密码
CentOS7 忘记root密码 长时间不用的 CentOS 机器再次开机的时候忽然忘记了密码,总不能就重装一台吧,还有好多服务在机器上,于是决定重置root的密码. 如果是已经开启的机器,需要进行关 ...
- error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler op
caffe c++11编译问题 问题:error: #error This file requires compiler and library support for the ISO C++ 201 ...
- 七、Java多人博客系统-2.0版本-docker部署
docker是当下很热门的技术,是对之前的部署系统方式的彻底改变.之前部署系统,需要安装数据库.初始化数据库,安装jdk,配置jdk,部署应用程序,修改配置文件等,很繁琐.一般现场运维人员很难搞定,现 ...
- Routing 为 Magento 2 一个重要的部分,本文介绍基本应用
Routing 为 Magento 2 一个重要的部分,本文介绍基本应用 Magento 2请求的流程 在Magento 2中,请求URL将如下所示: http://example.com/index ...
- LVM备份(1)-创建LVM逻辑卷
LV(Logical Volume) - 逻辑卷 VG(Volume Group) - 卷组 PV(Physical Volume) - 物理卷 1.查看分区信息:fdisk -l 可看到磁盘大小为1 ...
- python5 数字类型 字符串类型 列表类型
数字类型 # 了了解:py2中小整数用int存放,大整数用long # 1.整型 num = -1000000000000000000000000000000000000000000000000 ...
- 机器学习之正则化【L1 & L2】
前言 L1.L2在机器学习方向有两种含义:一是L1范数.L2范数的损失函数,二是L1.L2正则化 L1范数.L2范数损失函数 L1范数损失函数: L2范数损失函数: L1.L2分别对应损失函数中的绝对 ...