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 ...
随机推荐
- Mybatis逆向工程 —— ResultMaps collection already contains value for ***
报错提示: Result Maps collection already contains value for ***. 遭遇场景: maven+ssm 项目中,采用了mybatis的逆向工程生成 p ...
- js实现小功能 动态赋值
- Masonry 与 frame 混用导致的问题
https://www.jianshu.com/p/357fab4b84e7 Masonry 与 frame 混用可能出现子控件大小跟预期不一致的情况,具体是什么样呢? 例如,自定义一个 UIView ...
- json文件格式详解
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...
- 17.kubernete的dashboard
部署dashboard kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/depl ...
- Fixing “Did you mean to run dotnet SDK commands?” error when running dotnet –version
I recently installed the dotnet 1.11.0 Windows Server Hosting package which apparently installs the ...
- [洛谷P4234] 最小差值生成树
题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:求一棵生成树,其最大边权减最小边权最小 解题思路 和魔法森林非常像.先对所有边进行排序,每次加边的时候删除环上的最小 ...
- svn客户端更改用户名
你是用的小乌龟做客户端吗?在文件夹里点右键,选择TortoiseSVN->Setings->SavedData里面有个authentication data,点击后面的Clear就好了下次 ...
- ComboBox下拉列表框
属性:DropDownStyle(下拉.可编辑等).Items(条目) 事件:SelectedIndexChanged选择项改变触发 Items内容:总经理.副总经理.财务部 DropDownStyl ...
- Androidstudio 使用git插件提交代码
1.androidstudio中的项目已经推送到git仓库上(与仓库已经建立了联系) 参见: 2.右键目录--git---commit directory : 3.填写相应的commit Messig ...