默认字体

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。

 
image.png

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

 
image.png

将字体文件拷贝到font中

 
image.png

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开发之字体设置的更多相关文章

  1. Android开发之动态设置字体的样式和粗细

    字体设置通常有两种形式: 1:在xml中直接设置 android:textStyle="bold" android:typeface="sans" 2:用jav ...

  2. android开发之-软件设置保存-快速学会使用SharedPreferences篇-实测

    我们在设计软件的时候,需要记录软件设置的基本信息,那么怎么来保存他们呢?我们可以使用SharedPreferences.   SharedPreferences是一个xml文件,用来存储软件的常规设置 ...

  3. Android开发常用权限设置

    加在AndroidManifest.xml 文件中manifest标签以内,application以外 例如:<!--网络权限 --> <uses-permission androi ...

  4. Android APP的字体设置

    Android系统自带有对字体的设置,这些设置是对字体的显示方式的设置,比如加粗,倾斜,下划线,字号等,但是对于字体本身,比如设置为楷体,隶书等貌似没有.Android系统只有一种默认的,如果需要修改 ...

  5. Android SpannableString及字体设置

    import android.content.Context; import android.graphics.Color; import android.text.SpannableString; ...

  6. Android开发——Toolbar常用设置

    本篇笔记用来记录常用的Toolbar设置,如Toolbar颜色设置,显示返回按钮,显示右边三个点按钮 之前Android 使用的ActionBar,Android5.0开始,谷歌官方推荐使用Toolb ...

  7. android开发之当设置textview多少字后以省略号显示。限制TextView的字数

    设置多少字后以省略号显示 <TextView    android:id="@+id/tv"   android:layout_width="wrap_conten ...

  8. Android(java)学习笔记163:Android开发时候颜色设置是bgr不是rgb

    eb" android:background ="#" 

  9. [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)

    == 0) { convertView.setBackgroundResource(R.drawable.list_gray_9); } else { convertView.setBackgroun ...

随机推荐

  1. 关于for循环与setTimeout的延迟

    在for循环中使用setTimeout时,想使其每个setTimeout在上一个setTimeout的基础上进行延时,使用传入数组方式设置时间时,发现实际是按照每次设置的setTimeout的延时执行 ...

  2. BZOJ3328 PYXFIB 单位根反演

    题意:求 \[ \sum_{i=0}^n[k|i]\binom{n}{i}Fib(i) \] 斐波那契数列有简单的矩阵上的通项公式\(Fib(n)=A^n_{1,1}\).代入得 \[ =\sum_{ ...

  3. app.use和app.get,app.post区别

    express中,express的实例app: app.use(path,callback)中的callback既可以是router对象又可以是函数 app.get(path,callback)中的c ...

  4. 基于vue现有项目的服务器端渲染SSR改造

    前面的话 不论是官网教程,还是官方DEMO,都是从0开始的服务端渲染配置.对于现有项目的服务器端渲染SSR改造,特别是基于vue cli生成的项目,没有特别提及.本文就小火柴的前端小站这个前台项目进行 ...

  5. [SimplePlayer] 1. 从视频文件中提取图像

    在开始之前,我们需要了解视频文件的格式.视频文件的格式众多,无法三言两语就能详细分析其结构,尽管如此,ffmpeg却很好地提取了各类视频文件的共同特性,并对其进行了抽象描述. 视频文件格式,统称为co ...

  6. spring中设计模式

    MVC模式 Model:pojo.数据库交互(业务数据和业务逻辑) View:Jsp(与用户交互页面) Controller:控制器(接收请求并决定调用哪个模块组件去处理请求,然后决定调用哪个视图(通 ...

  7. GitHub最基本使用总结

    GitHub最基本使用入门 入门必看博客:https://mp.weixin.qq.com/s/LbzSwl4dYwrSPze0w10l8w 一.Git Linux安装 Git Linux安装教程:h ...

  8. Django+Vue打造购物网站(六)

    商品详情页功能 商品详情页和CategoryViewSet类似,只需要多继承一个类(mixins.RetrieveModelMixin)就可以了 class GoodsListViewSet(mixi ...

  9. Gym - 101606G Gentlebots

    Rainforest Inc. is opening a large new automated warehouse in the far Northern reaches of theUK—some ...

  10. Pandas系列(十二)-可视化详解

    目录 1. 折线图 2. 柱状图 3. 直方图 4. 箱线图 5. 区域图 6. 散点图 7. 饼图六边形容器图 数据分析的结果不仅仅只是你来看的,更多的时候是给需求方或者老板来看的,为了更直观地看出 ...