Android Studio 中的 Assets 文件夹? [英] Assets folder in Android Studio?

查看:54
本文介绍了Android Studio 中的 Assets 文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义字体,并且我读到我想将字体放在资产/字体中.我正在使用 Android Studio,但我似乎没有资产文件夹.所以我创建了一个.但是当我将资产文件夹放在 src/main 中时,我的应用程序崩溃了.我使用此代码加载我的字体.

Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one. But my app crashes when I place the assets folder in src/main. Im using this code to load my fonts.

Typeface fontRegular = Typeface.createFromAsset(getAssets(), "fonts/DroidSans.ttf");
Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/DroidSans-Bold.ttf");

myDeviceModelTxt.setTypeface(fontRegular);

我做错了什么?

推荐答案

我不确定自从提出这个问题后是否有任何错误修复,但我在 Android Studio 0.5.2 中的项目中使用当前结构:

I am not sure if there has been any bug fixes since this was asked, but I am using the current structure for a project in Android Studio 0.5.2:

root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------SomeFont.ttc
|----------AnotherFont.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle

然后调用获取

Typeface.createFromAsset(mContext.getResources().getAssets(), "SomeFont.ttc");

但请注意,有一个错误 (https://code.google.com/p/android/issues/detail?id=9904) 阻止字体被正确垃圾收集.使用单例!

Word of caution though, there is a bug (https://code.google.com/p/android/issues/detail?id=9904) that prevents typefaces from being garbage collected properly. Use a singleton!

这篇关于Android Studio 中的 Assets 文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆