Android开发之字体设置

一、前言:

1、默认字体

Android SDK自带了四种字体:"normal"“monospace",“sans”, “serif”,
如下:


字体.png

设置方式

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);

二、设置第三方字体

1、Res中使用字体

显示如图:


图片.png
  • 在res下创建font包,里面加入字体类型,就可以直接引用

java代码中使用

    TextView txtNormal = (TextView) findViewById(R.id.txt_normal);
    Typeface typeface = ResourcesCompat.getFont(this, R.font.bold);
    txtNormal.setTypeface(typeface);

XML布局使用:

<TextView
        android:id="@+id/tv_status"
        android:includeFontPadding="false"
        android:fontFamily="@font/bold"
        android:layout_toLeftOf="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在举手"
        />

2、Assets中使用

新建Assets及fonts目录,并将字体文件拷贝到fonts目录下:


图片.png

在java代码中使用

    TextView txtNormal = (TextView) findViewById(R.id.txt_helvetica);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");
    txtNormal.setTypeface(typeface);

三、第三方框架全局字体设置

  • 这里推荐一个第三方字体设置库Calligraphy,详细可以点击连接

参考链接://www.greatytc.com/p/d1e42218a1e2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容