国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

淺析Android加載字體包及封裝的方法

瀏覽:18日期:2022-09-24 13:28:01

TextView加載字體包

在 Android 中,若需要使得某個(gè)TextView加載字體包,使用以下方式即可:

Typeface typeFace =Typeface.createFromAsset(getAssets(),'fonts/Bold.otf'); textView.setTypeface(typeFace);

至于字體包的位置:

淺析Android加載字體包及封裝的方法

通過以上方法,可以使得一個(gè)TextView加載某種字體包,但是,還有這種需求:

部分TextView加載字體包 每個(gè)TextView加載的字體包不一定一樣

這時(shí),我們就需要稍微封裝下,將其封裝成一個(gè)自定義TextView類,若需要使用字體包,則加載該類,同時(shí),可以根據(jù)xml里面的值,從而加載不同的字體包。

封裝

定義屬性值

首先,我們需要從xml里面獲取值,因此,需要在attr中進(jìn)行屬性值的定義:

淺析Android加載字體包及封裝的方法

<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='測(cè)試' />

效果

淺析Android加載字體包及封裝的方法

到此這篇關(guān)于淺析Android加載字體包及封裝的方法的文章就介紹到這了,更多相關(guān)android加載字體包封裝內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 美女把张开腿男生猛戳免费视频 | 免费观看一级欧美在线视频 | 国产做国产爱免费视频 | 草草视频在线免费观看 | 亚洲深夜视频 | 日韩亚洲人成网站在线播放 | 成人a毛片一级 | 欧美性猛交xxxx免费看手交 | 视频一区在线免费观看 | 老妇综合久久香蕉蜜桃 | 永久毛片 | 三级毛片大全 | 亚洲自拍偷拍网 | 狠狠色丁香九九婷婷综合五月 | 国产精品_国产精品_国产精品 | 欧美亚洲国产精品久久 | 一区二区欧美视频 | 久久久久久久免费 | 亚洲女人在线 | 日韩一及片 | 成人黄页| japanese日本tube色系 | 热99re久久精品2久久久 | 国产成人区 | 日本欧美一区二区三区不卡视频 | 加勒比色综合久久久久久久久 | 成人免费网站视频 | 美女张开腿让男人桶爽免费网站 | 久久青草免费线观最新 | 久久精品亚洲精品国产欧美 | 国产一区二区三区免费大片天美 | 久久福利青草精品免费 | 深夜福利国产福利视频 | 日韩精品欧美激情国产一区 | 亚洲综合成人在线 | 末满18以下勿进色禁网站 | 国产第一亚洲 | 精品国产区一区二区三区在线观看 | 成人观看网站a | 成 人 在 线 免费 8888 www | 国产主播福利精品一区二区 |