解析Android中使用自定義字體的實(shí)現(xiàn)方法
1、Android系統(tǒng)默認(rèn)支持三種字體,分別為:“sans”, “serif”, “monospace
2、在Android中可以引入其他字體 。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:Android="http://schemas.android.com/apk/res/android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent" >
<TableRow>
<TextView
Android:layout_marginRight="4px"
Android:text="sans:"
Android:textSize="20sp" >
</TextView>
<!-- 使用默認(rèn)的sans字體 -->
<TextView
Android:id="@+id/sans"
Android:text="Hello,World"
Android:textSize="20sp"
Android:typeface="sans" >
</TextView>
</TableRow>
<TableRow>
<TextView
Android:layout_marginRight="4px"
Android:text="serif:"
Android:textSize="20sp" >
</TextView>
<!-- 使用默認(rèn)的serifs字體 -->
<TextView
Android:id="@+id/serif"
Android:text="Hello,World"
Android:textSize="20sp"
Android:typeface="serif" >
</TextView>
</TableRow>
<TableRow>
<TextView
Android:layout_marginRight="4px"
Android:text="monospace:"
Android:textSize="20sp" >
</TextView>
<!-- 使用默認(rèn)的monospace字體 -->
<TextView
Android:id="@+id/monospace"
Android:text="Hello,World"
Android:textSize="20sp"
Android:typeface="monospace" >
</TextView>
</TableRow>
<!-- 這里沒(méi)有設(shè)定字體,我們將在Java代碼中設(shè)定 -->
<TableRow>
<TextView
Android:layout_marginRight="4px"
Android:text="custom:"
Android:textSize="20sp" >
</TextView>
<TextView
Android:id="@+id/custom"
Android:text="Hello,World"
Android:textSize="20sp" >
</TextView>
</TableRow>
</TableLayout>
// 得到TextView控件對(duì)象
TextView textView = (TextView) findViewById(R.id.custom);
// 將字體文件保存在assets/fonts/目錄下,www.linuxidc.com創(chuàng)建Typeface對(duì)象
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/DroidSansThai.ttf");
// 應(yīng)用字體
textView.setTypeface(typeFace);
如果想對(duì)整個(gè)界面的所有控件都應(yīng)用自定義字體,可以:
package arui.blog.csdn.net;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FontManager {
public static void changeFonts(ViewGroup root, Activity act) {
Typeface tf = Typeface.createFromAsset(act.getAssets(),
"fonts/xxx.ttf");
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, act);
}
}
}
}
- Android使用自定義字體的方法
- Android Back鍵點(diǎn)擊兩次退出應(yīng)用詳解及實(shí)現(xiàn)方法總結(jié)
- Android 中SP與DP的區(qū)別實(shí)例詳解
- Android實(shí)現(xiàn)360手機(jī)助手底部的動(dòng)畫(huà)菜單
- 手把手教你用Android自定義餅狀圖
- Android獲取手機(jī)通話記錄的方法
- Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球
- Android Gradle Build Error:Some file crunching failed, see logs for details的快速解決方法
- Android仿新浪微博、QQ空間等帖子顯示(2)
- Android仿新浪微博、QQ空間等帖子顯示(1)
- Android APP使用自定義字體實(shí)現(xiàn)方法
相關(guān)文章
Kotlin中?和!!的區(qū)別詳細(xì)對(duì)比
這篇文章主要給大家介紹了關(guān)于Kotlin中?和!!區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

Android實(shí)現(xiàn)從底部彈出的Dialog的實(shí)例代碼

Android AbsoluteLayout和RelativeLayout布局詳解

使用IntelliJ IDEA 配置安卓(Android)開(kāi)發(fā)環(huán)境的教程詳解(新手必看)

SimpleCommand實(shí)現(xiàn)圖片下載(二)

android車(chē)牌識(shí)別系統(tǒng)EasyPR使用詳解