简体   繁体   English

如何在android中检索可用/已安装字体的列表?

[英]How to retrieve a list of available/installed fonts in android?

In Java I would do something like:在Java中,我会做类似的事情:

java.awt.GraphicsEnvironment ge = 
                      java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts(); 

is there an Android equivalent?有安卓版吗?

Taken from Mark Murphy's answer on the Android Developers mailing list:摘自 Mark Murphy 在 Android Developers 邮件列表上的回答:

http://developer.android.com/reference/android/graphics/Typeface.html http://developer.android.com/reference/android/graphics/Typeface.html

There are only three fonts: normal (Droid Sans), serif (Droid Serif), and monospace (Droid Sans Mono).只有三种字体:normal (Droid Sans)、serif (Droid Serif) 和 monospace (Droid Sans Mono)。

While there may be additional fonts buried in WebKit somewhere, they appear to be inaccessible to developers outside of WebKit.虽然 WebKit 中的某个地方可能隐藏着其他字体,但 WebKit 之外的开发人员似乎无法访问它们。 :-( :-(

The only other fonts are any TrueType ones you bundle with your application.唯一的其他字体是您与应用程序捆绑在一起的任何 TrueType 字体。

Edit: Roboto is a new font which came in with Android 4.0.编辑:Roboto 是 Android 4.0 中的一种新字体。 You can use this library project to use it in all versions back to API level 4 https://github.com/mcalliph/roboto-text-view您可以使用这个库项目在所有版本中使用它回到 API 级别 4 https://github.com/mcalliph/roboto-text-view

Regarding the actual question, here is a way to create a list of all available fonts installed:关于实际问题,这是一种创建已安装的所有可用字体列表的方法:

String path = "/system/fonts";
File file = new File(path);
File ff[] = file.listFiles();

Array ff[] will contain all the font files.数组 ff[] 将包含所有字体文件。

There are only 3 fonts available as part of android; android 中只有 3 种字体可用; normal (Droid Sans), serif (Droid Serif), and monospace (Droid Sans Mono).正常 (Droid Sans)、衬线 (Droid Serif) 和等宽字体 (Droid Sans Mono)。

Apps can include their own truetype fonts but can't install them for use by other apps.应用程序可以包含自己的 truetype 字体,但不能安装它们以供其他应用程序使用。

couple of links about the fonts:关于字体的几个链接:

This answer isn't a programmatic solution, but the actual ttf fonts seem to be stored in the /system/fonts directory.这个答案不是一个程序化的解决方案,但实际的 ttf 字体似乎存储在 /system/fonts 目录中。 Use adb shell ls /system/fonts to list them, or adb pull /system/fonts to transfer all of them to the connected computer (adb will create a folder named "fonts").使用adb shell ls /system/fonts列出它们,或adb pull /system/fonts将它们全部传输到连接的计算机(adb 将创建一个名为“fonts”的文件夹)。

From Android 29, giving you an actual collection of android.graphics.fonts.Font , there is SystemFonts.getAvailableFonts()从 Android 29 开始,为您提供android.graphics.fonts.Font的实际集合,有SystemFonts.getAvailableFonts()

https://developer.android.com/reference/kotlin/android/graphics/fonts/SystemFonts?hl=en#getAvailableFonts() https://developer.android.com/reference/kotlin/android/graphics/fonts/SystemFonts?hl=en#getAvailableFonts()

Android includes 3 base fonts, but unlike iOS, allow you to use just about any font you'd like. Android 包括 3 种基本字体,但与 iOS 不同的是,它允许您使用几乎任何您喜欢的字体。 You can simply embed it with your app, instead of being limited to a preset list of fonts like Apple does (Apple doesn't allow font embedding).您可以简单地将它嵌入到您的应用程序中,而不是像 Apple 那样受限于预设的字体列表(Apple 不允许字体嵌入)。 Pretty convenient.挺方便的。

Note that this is for Android itself, but web browsers (including the basic pre-installed Android web browser) does support all the standard HTML fonts.请注意,这是针对 Android 本身,但网络浏览器(包括基本预装的 Android 网络浏览器)确实支持所有标准 HTML 字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM