這篇文章將為大家詳細(xì)講解有關(guān)Android加載字體包及封裝的方法是什么,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、長寧網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為長寧等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
TextView加載字體包
在 Android 中,若需要使得某個TextView
加載字體包,使用以下方式即可:
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Bold.otf"); textView.setTypeface(typeFace);
至于字體包的位置:
通過以上方法,可以使得一個TextView
加載某種字體包,但是,還有這種需求:
TextView
加載字體包TextView
加載的字體包不一定一樣這時(shí),我們就需要稍微封裝下,將其封裝成一個自定義TextView
類,若需要使用字體包,則加載該類,同時(shí),可以根據(jù)xml
里面的值,從而加載不同的字體包。
封裝
定義屬性值
首先,我們需要從xml
里面獲取值,因此,需要在attr
中進(jìn)行屬性值的定義:
<declare-styleable name="FontTextView"> <attr name="fontType" format="enum"> <enum name="bold" value="1" /> <enum name="heavy" value="2" /> </attr> </declare-styleable>
這里我只定義了兩種屬性,大家可以根據(jù)需求進(jìn)行增減。
創(chuàng)建自定義TextView
public class FontTextView extends AppCompatTextView { public FontTextView(Context context) { super(context); } public FontTextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } }
獲取屬性值
//獲取參數(shù) TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyleAttr, 0); int fontType = a.getInt(R.styleable.FontTextView_fontType, 1);
進(jìn)行值判斷并加載不同的字體包
private final int BOLD = 1; private final int HEAVY = 2; String fontPath = null; switch (fontType) { case BOLD: fontPath = "fonts/Bold.otf"; break; case HEAVY: fontPath = "fonts/Heavy.otf"; break; default: } //設(shè)置字體 if (!TextUtils.isEmpty(fontPath)) { Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontPath); setTypeface(typeFace); }
全部源碼
public class FontTextView extends AppCompatTextView { private final int BOLD = 1; private final int HEAVY = 2; public FontTextView(Context context) { super(context); } public FontTextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //獲取參數(shù) TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyleAttr, 0); int fontType = a.getInt(R.styleable.FontTextView_fontType, 1); String fontPath = null; switch (fontType) { case BOLD: fontPath = "fonts/Bold.otf"; break; case HEAVY: fontPath = "fonts/Heavy.otf"; break; default: } //設(shè)置字體 if (!TextUtils.isEmpty(fontPath)) { Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), fontPath); setTypeface(typeFace); } } }
若需要使用字體包TextView
,使用以下方式即可:
<com.jm.core.common.widget.textview.FontTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:fontType="bold" android:text="測試" />
效果
關(guān)于Android加載字體包及封裝的方法是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
新聞名稱:Android加載字體包及封裝的方法是什么
URL分享:http://aaarwkj.com/article32/jescsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、云服務(wù)器、響應(yīng)式網(wǎng)站、定制開發(fā)、網(wǎng)站內(nèi)鏈、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)