国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

解析Android中使用自定義字體的實現方法

2020-02-21 17:25:01
字體:
來源:轉載
供稿:網友

Android系統(tǒng)的默認字體很多都不一樣,很多字體都是由手機廠商定制的,下面就讓武林技術頻道小編為大家介紹Android中使用自定義字體的實現方法,希望能幫到您。

1、Android系統(tǒng)默認支持三種字體,分別為:“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>
??????? <!-- 使用默認的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>
??????? <!-- 使用默認的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>
??????? <!-- 使用默認的monospace字體 -->

??????? <TextView
??????????? Android:id="@+id/monospace"
??????????? Android:text="Hello,World"
??????????? Android:textSize="20sp"
??????????? Android:typeface="monospace" >
??????? </TextView>
??? </TableRow>
??? <!-- 這里沒有設定字體,我們將在Java代碼中設定 -->

??? <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控件對象
TextView textView = (TextView) findViewById(R.id.custom);
// 將字體文件保存在assets/fonts/目錄下,www.linuxidc.com創(chuàng)建Typeface對象
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/DroidSansThai.ttf");
// 應用字體
textView.setTypeface(typeFace);


如果想對整個界面的所有控件都應用自定義字體,可以:

?

?

?


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中使用自定義字體的實現方法了吧,我們可以按照上述的介紹進行執(zhí)行,相信你是最棒的。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湖南省| 岳阳县| 水城县| 基隆市| 中超| 凤山市| 轮台县| 乌恰县| 婺源县| 神池县| 隆昌县| 循化| 禄劝| 通榆县| 泰州市| 修文县| 会东县| 买车| 措美县| 凤庆县| 韶关市| 宁波市| 澄迈县| 普陀区| 黔西县| 兴和县| 梁山县| 长兴县| 射洪县| 留坝县| 荥阳市| 邯郸市| 惠东县| 宜丰县| 合山市| 保德县| 辉县市| 沐川县| 米林县| 伊通| 兴城市|