我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!
当前回答
将字体添加到app/src/main/assets的assets文件夹中 像这样创建一个自定义文本视图:
class CustomLightTextView : TextView {
constructor(context: Context) : super(context){
attachFont(context)
}
constructor(context: Context, attrs: AttributeSet): super(context, attrs){
attachFont(context)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
attachFont(context)
}
fun attachFont(context: Context) {
this.setTypeface(FontCache.getInstance().getLightFont(context))
}
}
添加FontCache:这样你就不必像这样一遍又一遍地创建字体:
class FontCache private constructor(){
val fontMap = HashMap<String,Typeface>()
companion object {
private var mInstance : FontCache?=null
fun getInstance():FontCache = mInstance?: synchronized(this){
return mInstance?:FontCache().also { mInstance=it }
}
}
fun getLightFont(context: Context):Typeface?{
if(!fontMap.containsKey("light")){
Typeface.createFromAsset(context.getAssets(),"Gotham-Book.otf");
fontMap.put("light",Typeface.createFromAsset(context.getAssets(),"Gotham-Book.otf"))
}
return fontMap.get("light")
}
}
这样就完成了!
附注:从android O,你可以直接添加字体。
其他回答
在项目中添加字体
添加字体作为资源,在Android Studio中执行以下步骤:
1 -右键单击res文件夹,进入New > Android资源目录。 出现“新建资源目录”窗口。
2 -在“资源类型”列表中,选择“字体”,单击“确定”。 3 -添加您的字体文件在字体文件夹只是一个简单的复制和粘贴。注意字体的名称应该是小写的。
使用XML布局中的字体
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
向样式添加字体
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>
</style>
以编程方式使用字体
科特林:
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface
JAVA:
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
我无法加载字体,因为我将字体文件命名为Poppins-Medium。Tff,重命名为poppins_medium。TFF对我很管用。 其余步骤保持不变:
在res文件夹下创建字体资源目录 将tff文件复制并粘贴到该目录中 然后在XML的TextView中使用fontFamily属性。 如果以上步骤不起作用,您可以使用此链接创建该字体的FontFamily
2021年更新:
在res文件夹中创建一个名为font的文件夹并复制您的字体
所有字体名称只能为:小写字母a-z、0-9或下划线。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/abc_font" />
编程使用:
textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))
对于Android Studio 4.2+,现在甚至有一个菜单选项:
在Project -> app(或您的app名称)-> src -> main ->右键单击->新建->目录中创建文件夹资产。 然后在资产中创建一个名为fonts的新目录。
将字体分配给textView:
TextView textView = (TextView) findViewById(R.id.your_textView);
final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");
Your_font_name包含字体扩展名。
您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。您将从创建资产文件夹也自由。
简单:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库提供了许多类型的字体。
推荐文章
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)