基本上,我试图将ttf字体导入到库项目中。但我遇到了一些问题。由于您无法在assets / fonts文件夹中导入ttf,因此我尝试在res / raw中导入它们。一直在寻找解决方案,但似乎没有什么具体的,因为大多数人只会导入非库项目并使用Typeface.createFromAsset。
我在使用自定义视图将它们加载到xml时遇到了一些困难:
<com.demo.helpers.TextFontView
android:id="@+id/login_title_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mob_LogIn"
android:textColor="@color/white"
android:textSize="@dimen/font_login"
demo:fontFace="+id/opensans-regular"
/>
尝试在TextFontView子类中实现以下代码,但是我在尝试在之前的xml中链接它时遇到了一些困难:
public static Typeface getgetTypefaceFromRes(Context context, int resource)
{
Typeface typeFace = null;
InputStream is = null;
try {
is = context.getResources().openRawResource(resource);
}
catch(NotFoundException e) {
Log.e("Typeface", "Could not find font in resources!");
}
String outPath = context.getCacheDir() + "/tmp" + System.currentTimeMillis() + ".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();
typeFace = Typeface.createFromFile(outPath);
// clean up
new File(outPath).delete();
}
catch (IOException e)
{
Log.e("Typeface", "Error reading in font!");
return null;
}
Log.d("Typeface", "Successfully loaded font.");
return typeFace;
}