Android 如何在安卓设备中安装自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10051915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install custom font in android device
提问by kalpana c
I want to install custom font which is .ttf file in android device when my application start. Actually I need to install Burmese font in my android device when I run my application. Is it possible? If yes, then how? I don't need typeface. I want to install in system.
我想在我的应用程序启动时在 android 设备中安装自定义字体,即 .ttf 文件。实际上,当我运行我的应用程序时,我需要在我的 android 设备中安装缅甸字体。是否可以?如果是,那么如何?我不需要字体。我想安装在系统中。
回答by ρяσ?ρ?я K
See here@CommonsWare answer, You cannot add fonts to an existing device, except as part of a custom firmware build, or possibly by rooting the device.
请参阅 此处@CommonsWare 的答案,您不能将字体添加到现有设备,除非作为自定义固件构建的一部分,或者可能通过对设备进行 root 操作。
but you can add fonts in your appliction. like:
但您可以在应用程序中添加字体。喜欢:
TextView txtvw=(TextView)findViewById(R.id.textviewname);
Typeface typface=Typeface.createFromAsset(getAssets(),"fonts/burmese.ttf");
txtvw.setTypeface(typface);
Note:put your font in assets/fonts
dir in project
注意:将您的字体assets/fonts
放在项目中的目录中
ORif your device is rooted then see this tuts for installing Custom Fonts on Your Android Device
或者,如果您的设备已植根,请参阅此 tuts 以在您的 Android 设备上安装自定义字体
回答by UVM
you need to be on root of the device
你需要在设备的根目录下
for that you can use this application EasyRoot.apk
为此,您可以使用此应用程序 EasyRoot.apk
Download and install ES File Explorer (https://market.android.com/details?id=com.estrongs.android.pop)
下载并安装 ES File Explorer (https://market.android.com/details?id=com.estrongs.android.pop)
After that you need to enable root explorer. More info can obtained from this link
之后,您需要启用根资源管理器。可以从此链接获得更多信息
save fonts on your device's /system/font folder
将字体保存在设备的 /system/font 文件夹中
More info can obtained from this link
可以从此链接获得更多信息
回答by Shankar Agarwal
if you have custom font then use following code:::
如果您有自定义字体,请使用以下代码:::
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
tv.setTypeface(face);
also place your font in assets/fonts folder
还将您的字体放在 assets/fonts 文件夹中
回答by Harsh
I created a FontWrapper Class for my project.
我为我的项目创建了一个 FontWrapper 类。
public static Typeface getTypeface(Context c, Fonts name) {
synchronized (typefaces) {
if (!typefaces.containsKey(name)) {
try {
String fontPath = getFontsPath(name);
if (!StringUtil.isNullOrEmpty(fontPath)) {
InputStream inputStream = c.getAssets().open(fontPath);
File file = createFileFromInputStream(inputStream);
if (file == null) {
return Typeface.DEFAULT;
}
Typeface t = Typeface.createFromFile(file);
typefaces.put(name, t);
} else {
return Typeface.DEFAULT;
}
} catch (Throwable e) {
e.printStackTrace();
return Typeface.DEFAULT;
}
}
return typefaces.get(name);
}
}
private static File createFileFromInputStream(InputStream inputStream) {
try {
File f = File.createTempFile("font", null);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
return f;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}