android app 如何設置自己喜歡的字體

android系統提供了三種默認的字體樣式:bold, normal , italic. 如果你的應用對字體有特殊要求怎麼辦呢?下面簡單說一下具體的操作步驟:

方法一、通過繼承TextView等weiget控件
1>在/asset/目錄下存放字體文件:/asset/font/myFont.ttf

2>繼承實現自己的TextView控件:

public class TitleTextView extends TextView {

    public TitleTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        init(context);
    }

    public TitleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init(context);
    }

    public TitleTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init(context);
    }
   
    private void init(Context context){
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/myFont.ttf");
        this.setTypeface(tf);
    }  
}

3>在你的佈局文件中使用即可。
               style="@style/PriceOzTextStyleGreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center_vertical"
            android:text="@string/title_account_center"
            android:textSize="@dimen/text_size_large"
            android:textStyle="bold" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章