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

您的位置:首頁技術文章
文章詳情頁

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

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

TextView加載字體包

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

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

至于字體包的位置:

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

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

部分TextView加載字體包 每個TextView加載的字體包不一定一樣

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

封裝

定義屬性值

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

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

<declare-styleable name='FontTextView'> <attr name='fontType' format='enum'> <enum name='bold' value='1' /> <enum name='heavy' value='2' /> </attr> </declare-styleable>

這里我只定義了兩種屬性,大家可以根據需求進行增減。

創建自定義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); }}

獲取屬性值

//獲取參數 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyleAttr, 0); int fontType = a.getInt(R.styleable.FontTextView_fontType, 1);

進行值判斷并加載不同的字體包

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: } //設置字體 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); //獲取參數 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: } //設置字體 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='測試' />

效果

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

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

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲视频在线网 | 三级在线网站 | 91国内精品久久久久影院优播 | 日韩欧美中文字幕一区二区三区 | 国产成人a视频在线观看 | 日本免费视频观看在线播放 | 久久久一级| 国产成人免费不卡在线观看 | 99久久综合| 欧美成人性色区 | 国产超清在线观看 | 欧美日韩在线永久免费播放 | 欧美一级毛片欧美大尺度一级毛片 | 久久频这里精品99香蕉久 | 国产特黄特色一级特色大片 | 日韩精品久久久久久 | 国产欧美一区二区 | 久久不见久久见免费影院www日本 | 亚洲美女免费视频 | 亚洲一区二区三区四区五区 | 成人午夜在线观看 | 亚洲伊人色综合网站亚洲伊人 | 欧美1| 国产亚洲精品福利片 | 国产综合精品久久亚洲 | 免费看国产精品久久久久 | 日韩一区二区三区免费视频 | 免费观看一级特黄三大片视频 | 国产三香港三韩国三级不卡 | 亚洲精品国产一区二区 | 九九午夜 | 亚洲欧美成人综合 | 香蕉久久综合精品首页 | 国产偷国产偷亚洲高清午夜 | 日本在线视频播放 | 亚洲精品欧美精品 | 日韩精品毛片 | 精品一区二区三区三区 | 欧美成人性色生活片免费在线观看 | 久久精品国产精品青草色艺 | 久草成人在线视频 |