我试图在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,你可以直接添加字体。
其他回答
您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。您将从创建资产文件夹也自由。
简单:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
这个库提供了许多类型的字体。
我想补充一下我对Android- o和Android Studio 2.4的回答
在res文件夹下创建名为font的文件夹。下载要添加到项目示例谷歌fonts中的各种字体 在xml用户字体家族中 例子: < TextView android: fontFamily = " @font / indie_flower” android: layout_width = " wrap_content " android: layout_height = " wrap_content " android:填充= " 10 dp” android:文本= " @string / sample_text " / >
3.如果您希望它以编程的方式使用下面的代码
Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);
要了解更多信息,请链接到我的博客文章字体样式的Android与Android Studio 2.4
对于新读者
你可以使用这个库 Gloxey定制字体视图
gradle依赖
dependencies{
compile 'io.gloxey.cfv:custom-font-views:1.0.2'
}
如何使用?
创建文件夹资产->字体。复制你的字体到字体文件夹。
使用属性app: font_name = "font_name_string"在视图上应用字体。
例子
<!--Font Names in srings.xml-->
<string name="aadhunik">aadhunik.ttf</string>
<string name="kung_fool">kungfool.ttf</string>
<string name="skrova">skrova.otf</string>
<string name="painting_in_the_sun_light">painting_in_the_sun_light.ttf</string>
<!--Include views in layout.xml-->
<io.gloxey.cfv.CFTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Aadhunik"
android:textColor="#ff00"
android:textSize="40sp"
app:font_name="@string/aadhunik" />
<io.gloxey.cfv.CFButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Kung Fool"
android:textColor="#154748"
app:font_name="@string/kung_fool" />
<io.gloxey.cfv.CFEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello world"
android:textSize="30sp"
app:font_name="@string/skrova" />
<io.gloxey.cfv.CFCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Painting In The Sun Light"
android:textSize="30sp"
app:font_name="@string/painting_in_the_sun_light" />
把字体放在资产文件夹 然后应用fontfamily: " your fonts
首先创建资产文件夹,然后在其中创建字体文件夹。
然后你可以设置字体从资产或目录如下:
public class FontSampler extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.custom);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv.setTypeface(face);
File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");
if (font.exists()) {
tv = (TextView) findViewById(R.id.file);
face = Typeface.createFromFile(font);
tv.setTypeface(face);
} else {
findViewById(R.id.filerow).setVisibility(View.GONE);
}
}
}
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。