當前位置:首頁 » 安卓系統 » typefaceandroid

typefaceandroid

發布時間: 2022-08-02 21:59:31

Ⅰ 關於android自定義字體,該怎麼處理

解決方案

1)Android默認方法 #1

你可以通過ID查找到View,然後挨個為它們設置字體。在單個View的情況下,它看起來也沒有那麼可怕。

Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/YourCustomFont.ttf");
TextView view = (TextView) findViewById(R.id.activity_main_header);
view.setTypeface(customFont);
但是在很多TextView、Button等文本組件的情況下,我敢肯定你不會喜歡這個方法的。:D

2)Android默認方法 #2

你可以為每個文本組件創建一個子類,如TextView、Button等,然後在構造函數中載入自定義字體。

public class BrandTextView extends TextView {

public BrandTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BrandTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BrandTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont_Bold.ttf"));
} else {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont.ttf"));
}
}
}
然後只需要將標準的文本控制項替換成你自定義的就可以了(例如BrandTextView替換TextView)。

<com.your.package.BrandTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View with custom font"/>
<com.your.package.BrandTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="View with custom font and bold typeface"/>
還有,你甚至可以直接在XML中添加自定義的字體屬性。要實現這個,你需要定義你自己的declare-styleable屬性,然後在組件的構造函數中解析它們。

為了不佔篇幅介紹這么基礎的東西,這里有一篇不錯的文章告訴你怎麼自定義控制項屬性。

http://kevindion.com/2011/01/custom-xml-attributes-for-android-widgets/
在大多數情況下,這個方法還不賴,並且有一些優點(例如,切換字體粗細等等,字體可以在組件xml文件的typeface屬性中定義)。但是我認為這個實現方法還是太重量級了,並且依賴大量的模板代碼,為了一個替換字體的簡單任務,有點兒得不償失。

3)我的解決方案

理想的解決方案是自定義主題,然後應用到全局或者某個Activity。
但不幸的是,Android的android:typeface屬性只能用來設置系統內嵌的字體,而非用戶自定義字體(例如assets文件中的字體)。這就是為什麼我們無法避免在java代碼中載入並設置字體。

所以我決定創建一個幫助類,使得這個操作盡可能的簡單。使用方法:

FontHelper.applyFont(context, findViewById(R.id.activity_root), "fonts/YourCustomFont.ttf");
並且這行代碼會用來載入所有的基於TextView的文本組件(TextView、Button、RadioButton、ToggleButton等等),而無需考慮界面的布局層級如何。

標准(左)與自定義(右)字體的用法。

Standard (left) and Custom (right) fonts usage.

這是怎麼做到的?非常簡單:

public static void applyFont(final Context context, final View root, final String fontName) {
try {
if (root instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); i++)
applyFont(context, viewGroup.getChildAt(i), fontName);
} else if (root instanceof TextView)
((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName));
} catch (Exception e) {
Log.e(TAG, String.format("Error occured when trying to apply %s font for %s view", fontName, root));
e.printStackTrace();
}
}
正如你所看到的,所需要做的僅僅是將基於TextView的文本組件從布局中遍歷出來而已。

Ⅱ 為什麼android上設置了font-family就顯示不了了

// 自定義字體
custom = new TextView(this);
//xx.ttf located at assets/fonts/
typeface = Typeface.createFromAsset(getAssets(),"fonts/xx.ttf");
custom.setTypeface(typeface);

.自定義字體
1.android Typeface使用TTF字體文件設置字體

我們可以在程序中放入ttf字體文件,在程序中使用Typeface設置字體。
第一步,在assets目錄下新建fonts目錄,把ttf字體文件放到這。
第二步,程序中調用:
AssetManager mgr=getAssets();//得到AssetManager
Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");//根據路徑得到Typeface
tv.setTypeface(tf);//設置字體

2.在xml文件中使用android:textStyle=」bold」 可以將英文設置成粗體, 但是不能將中文設置成粗體,
將中文設置成粗體的方法是:
TextView tv = (TextView)findViewById(R.id.TextView01);
tv.getPaint().setFakeBoldText(true);//中文仿「粗體」--使用TextPaint的仿「粗體」設置setFakeBoldText為true。

安卓手機怎麼打出凹凸文

答:在android中是可以改變字體樣式的!下載你需要的字體到assets文件夾下然後用TextView.setTypeface(Typeface typeface)來從資源文件中載入字體文件。

Ⅳ android布局文件怎麼設置字體

在windows操作系統中可以按照如下方法為android studio設置字體(包括字體大小,字體顏色)。

1、首先創建一個android文件,並打開XML布局文件,如下圖:

Ⅳ 如何在Android開發中使用自定義的字體庫

1、Android系統默認支持三種字體,分別為:「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創建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 開發中怎麼使用自定義字體

在Eclipse中新建Android工程fontdemo。代碼很簡單,只有MainActivity.java和CustomFontTextView.java。布局文件是activity_main.xml。assets下面是我們要使用的字體庫文件。

MainActivity只是負責展示我們自定義的這個TextView,運行可以啦。


熱點內容
ipad文件加密 發布:2024-05-02 08:20:30 瀏覽:442
粉土壓縮模量 發布:2024-05-02 07:53:59 瀏覽:805
國都證券初始密碼是多少 發布:2024-05-02 07:46:39 瀏覽:109
shell腳本和linux命令行 發布:2024-05-02 07:37:54 瀏覽:968
自己的伺服器搭建微信小程序商城 發布:2024-05-02 07:36:26 瀏覽:426
php單行注釋 發布:2024-05-02 07:36:22 瀏覽:958
買車哪些配置必備 發布:2024-05-02 07:30:20 瀏覽:52
華為手機的自帶鈴聲文件夾 發布:2024-05-02 07:20:14 瀏覽:501
xp系統開機密碼怎麼設置 發布:2024-05-02 06:49:48 瀏覽:759
柱加密區公式 發布:2024-05-02 06:40:19 瀏覽:4