android app 如何设置自己喜欢的字体

来源:互联网 发布:淘宝五皇冠店铺排名 编辑:程序博客网 时间:2024/06/09 17:00

http://blog.sina.com.cn/s/blog_8a86f4dd0101j18w.html


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" />

0 0
原创粉丝点击