日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android字体文件制作教程,Android使用自定义字体

發布時間:2023/12/18 Android 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android字体文件制作教程,Android使用自定义字体 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? 安卓開發使用自定義字體應該已經是司空見慣了,今天就來介紹一下如何使用自定義字體的方法。

準備工作

如下圖,首先創建一個assets文件夾,然后新建一個fonts子文件夾,放入字體文件。

微信截圖_20170816115004.png

方法一:通過重寫TextView實現在xml中配置

1.重寫TextView

private Context mContext;

private String TypefaceName = "";

public String getTypefaceName() {

return TypefaceName;

}

public void setTypefaceName(String typefaceName) {

TypefaceName = typefaceName;

Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

System.gc();

}

public FontTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

this.mContext = context;

int resouceId = attrs.getAttributeResourceValue(null, "typefaceName", 0);

if (resouceId != 0) {

TypefaceName = context.getResources().getString(resouceId);

} else {

TypefaceName = attrs.getAttributeValue(null, "typefaceName");

}

if (TypefaceName != null && !"".equals(TypefaceName)) {

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

}

}

public FontTextView(Context context, AttributeSet attrs) {

super(context, attrs);

this.mContext = context;

// 先判斷是否配置的資源文件

int resouceId = attrs.getAttributeResourceValue(null, "typefaceName", 0);

if (resouceId != 0) {

TypefaceName = context.getResources().getString(resouceId);

} else {

TypefaceName = attrs.getAttributeValue(null, "typefaceName");

}

if (TypefaceName != null && !"".equals(TypefaceName)) {

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

}

}

public FontTextView(Context context) {

super(context);

this.mContext = context;

// TypefaceName = attrs.getAttributeValue(null, "TypefaceName");

if (TypefaceName != null && !"".equals(TypefaceName)) {

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");

this.setTypeface(typeface);

}

}

}

2.xml使用typefaceName屬性引用字體文件

android:layout_width="60dp"

android:layout_height="50dp"

android:textSize="20sp"

typefaceName="Simhei"

android:text="This is first child view"/>

方法二:代碼內部設置

加載字體資源文件:

需要注意的是,字體文件加載不要放在主線程中,會阻塞線程,影響性能

public static Typeface typeFace =Typeface.createFromAsset(getContext().getAssets(),

"fonts/Simhei.ttf");

public static Typeface typeFaceArial= Typeface.createFromAsset(getContext().getAssets(),

"fonts/Arial.ttf");

使用:

TextView.setTypeface(typeFace);

是不是很簡單了,兩種方法任意選擇。

總結

以上是生活随笔為你收集整理的android字体文件制作教程,Android使用自定义字体的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。