Android库中的字体 [英] Font in Android Library
问题描述
在以下链接上
写为:
图书馆项目不能包含原始资产这些工具不支持在库项目中使用原始资产文件(保存在资产/目录中).应用程序使用的任何资产资源都必须存储在应用程序项目本身的asset/目录中.但是,支持保存在res/目录中的资源文件.
Library projects cannot include raw assets The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
因此,如果我想创建一个使用自定义字体的自定义视图组件,该如何访问资源?我不能用我喜欢的字体重新分发我的组件!!!!
So if I want to create a custom view component that use a custom font how can I access the resource? Can't I redistribute my component with my favorite font !!!!
最诚挚的问候
推荐答案
好,我找到了解决该问题的方法.您需要将文件复制到外部目录,然后使用 Typeface.createFromFile
从文件加载字体,然后删除临时文件.我知道这不是一种干净的工作方式,而是工作炉排.
Ok I have found a workaround for the problem. You need to copy the file to an external directory then load a typeface from file with Typeface.createFromFile
and then delete the temporary file. I know is not a clean mode of work but is working grate.
1-您需要将字体放在"/res/raw/font.ttf"上
1 - You need to put your font on "/res/raw/font.ttf"
2-在代码中插入以下方法
2 - Inser in your code the following method
3-输入您的代码 Typeface mFont = FileStreamTypeface(R.raw.font);
4-全部完成
Typeface FileStreamTypeface(int resource)
{
Typeface tf = null;
InputStream is = getResources().openRawResource(resource);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/gmg_underground_tmp";
File f = new File(path);
if (!f.exists())
{
if (!f.mkdirs())
return null;
}
String outPath = path + "/tmp.raw";
try
{
byte[] buffer = new byte[is.available()];
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath));
int l = 0;
while((l = is.read(buffer)) > 0)
{
bos.write(buffer, 0, l);
}
bos.close();
tf = Typeface.createFromFile(outPath);
File f2 = new File(outPath);
f2.delete();
}
catch (IOException e)
{
return null;
}
return tf;
}
如果有人有其他选择,我很高兴阅读.您是否要记住,此解决方法仅适用于Android库
if someone have an alternative I'm pleased to read it. Do you have to remember that this workaround is only for Android Libraries
最好的问候
这篇关于Android库中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!