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 ...
随机推荐
- 软工+C(3): 超链接
// 上一篇:分数和checklist // 下一篇:Alpha/Beta换人 注:平常看文章,总有能和构建之法,软件工程相关的链接,增量记录,也可以通过在其他人博客的交流中使用相关的超链接,在使用中 ...
- 控制结构(4): 局部化(localization)
// 上一篇:状态机(state machine) // 下一篇:必经之地(using) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上一次,我们说到状态机结构( ...
- mysql 5.7 json
项目中使用的mysql5.6数据库,数据库表一张表中存的字段为blob类型的json串数据.性能压测中涉及该json串处理效率比较低,开发人员提到mysql5.7版本后json串提供了原生态的json ...
- "unexpected console statement” in Node.js
.eslintrc.js module.exports = { rules: { 'no-console': 'off', }, };
- JS判断浏览器是否支持触屏事件
var hasTouch=function(){ var touchObj={}; touchObj.isSupportTouch = "ontouchend" in docume ...
- 基于stm32智能车的设计(ucosiii)---北京之行
实物演示视频:https://v.youku.com/v_show/id_XMzc3MDE3NjMyNA==.html?x&sharefrom=android&sharekey=172 ...
- 如何学习FPGA
如何学习FPGA 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/k331922164/article/details/44626989 PS:笔者强 ...
- 【并发编程】【JDK源码】J.U.C--AQS (AbstractQueuedSynchronizer)(1/2)
J.U.C实现基础 AQS.非阻塞数据结构和原子变量类(java.util.concurrent.atomic包中的类),concurrent包中的基础类都是使用这种模式来实现的.而concurren ...
- kafka partition(分区)与 group
kafka partition(分区)与 group 一. 1.原理图 2.原理描述 一个topic 可以配置几个partition,produce发送的消息分发到不同的partition中,co ...
- LOJ#2339 通道
题意:给你三棵树,求所有点对在三棵树上的距离和中的最大值. 解:首先有个暴力,然后还有个迭代乱搞,可以得到61分... namespace bf { inline void solve() { ; i ...