Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓使用自定义字体 #194

Open
soapgu opened this issue Mar 11, 2023 · 0 comments
Open

安卓使用自定义字体 #194

soapgu opened this issue Mar 11, 2023 · 0 comments
Labels
安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Mar 11, 2023

  • 前言

现在的字体已经很丰富了,问题安卓系统预装的字体并不完善,特别对于中文的一些字体是默认没有的。那我们要怎么在app中实现显示自定义的字体那?

  • 应用

  1. 从网上下载ttf格式的字体
  2. 右键点击 res 文件夹,然后转到 New > Android resource directory。
  3. 此时将显示“New Resource Directory”窗口。在“Resource type”列表中,选择“font”,然后点击 OK
  4. 把ttf文件手工拷贝到font目录下

图片

  1. 在TextView里面定义fontFamily,直接通过“@font/XXX”引用
<TextView
                android:textColor="@color/white"
                android:text="思源——Regular!"
                android:fontFamily="@font/sy_regular"
                android:textSize="40sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:layout_marginTop="80dp"
                android:textColor="@color/white"
                android:text="思源——Medium!"
                android:fontFamily="@font/sy_medium"
                android:textSize="40sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:layout_marginTop="160dp"
                android:textColor="@color/white"
                android:text="思源——Heavy!"
                android:fontFamily="@font/sy_heavy"
                android:textSize="40sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
  • 创建字体系列

上面的用法常用的够了,不过相对正规一点还是要创建字体系列
定义一个xml来引用不同的font,算是一个字体筛选器,可以通过设置不同属性,精确匹配。这里先不展开了

也可以用代码方式,当然肯定是xml定义用起来更方便

Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
@soapgu soapgu added the 安卓 安卓 label Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant