Android 使用自定义字体

整个项目要使用第三方字体首先将字体文件放到assets文件夹下

因为整个项目要用第三方字体这里我重写了 TextView Button EditText 三个控件

以TextView 为例代码如下  其它控件一样换下继承

public class CustomTextView extends TextView { 

    public CustomTextView(Context context) {
        super(context);
        init(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defSyle) {
        super(context, attrs, defSyle);
        init(context);
    }

    /***
     * 设置字体
     *
     * @return
     */
    public void init(Context context) {
        setTypeface(FontCustom.setFont(context));

    }
}

public class FontCustom {

    static String fongUrl = "fonts/fzltxh_gbk.ttf";
    static Typeface tf;

    /***
     * 设置字体
     *
     * @return
     */
    public static Typeface setFont(Context context) {
        if(tf==null){
            tf = Typeface.createFromAsset(context.getAssets(), fongUrl);
        }
        return tf;
    }
}

使用方法

<CustomTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="我是自定义字体"
                android:textColor="@color/login_font_hit"
                android:textSize="14.0sp" />

这样就实现了项目字体统一风格

时间: 2024-08-08 00:15:29

Android 使用自定义字体的相关文章

Android实现自定义字体

介绍 最近在看开源项目的时候,发现里面涉及到了自定义字体,虽然自己目前还用不到,但是动手demo笔记记录一下还是有必要的,没准哪天需要到这个功能. 原理 1.其实实现起来非常简单,主要是用到了Typeface这个类,通过加载assets里的ttf字体,调用View.setTypeface实现原生字体替换. 默认自带样式 public static final int NORMAL = 0; public static final int BOLD = 1; public static final

[Android] 如何自定义字体

项目里要统一用设计师的字体,android:typeface只支持系统三种字体.有什么比较好的做法? 你需要为整个应用替换自定义字体. 解决方案 1)Android默认方法 #1 你可以通过ID查找到View,然后挨个为它们设置字体.在单个View的情况下,它看起来也没有那么可怕. Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/YourCustomFont.ttf"); TextView

#Android 自定义字体样式

Android中自定义字体设置一般通过 facetype属性进行设置,先看一下官网提供的方法 顾名思义 就是说我们可以通过使用项目中assets文件下的资产文件或者是android本身的系统文件进行字体设置. 如果使用assets方法的话,首先我们需要在项目路径下创建assets文件夹, 如图所示,设置好文件之后,可以使用 Typeface typeface1 = Typeface.createFromAsset(this.getAssets(),"fonts/1.TTF"); tvT

Android 开发使用自定义字体

有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在 assets 目录下. 自定义字体文件不能使用xml代码读取而应该使用java代码: public class MyActivity extends Activity { private TextView mText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance

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

Android SearchView 自定义SearchIcon和字体颜色大小

自定义SearchView的搜索图标和字体属性相对复杂一些,记下来. 一.自定义SearchIcon 1.API版本低于21:版本小于21时,要修改SearchIcon比较复杂,需要先获取到SearchView的ImageView,然后为ImageView设置图片,具体代码如下: (1)初始化SearchView控件 mSearch = (SearchView) view.findViewById(R.id.search); (2)设置自定义的搜索图标 if(mSearch==null){ re

Android:更好的自定义字体方案

http://ryanhoo.github.io/blog/2014/05/05/android-better-way-to-apply-custom-font/ 情景 解决方案 1)Android默认方法 #1 2)Android默认方法 #2 3)我的解决方案 译者注 参考 原文:http://vision-apps.blogspot.hk/2012/02/android-better-way-to-apply-custom-font.html 在一个应用中,我需要在所有的UI组件中使用客户

Android中快速实现自定义字体!

前言:我们都知道,Android中默认的字体是黑体,而大多数app也都是使用的这种字体,但我们发现,大多数app中,个别地方字体非常好看,例如app的标题栏,菜单栏等地方,那他们是怎么做到的呢?有两种方式,第一是图片来代替文字,第二,就是今天我要教大家的自定义字体. 开发环境: Android Studio 2.2.2 compileSdkVersion 25 buildToolsVersion "25.0.0" minSdkVersion 19 targetSdkVersion 25

css3 自定义字体的使用方法

@font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许多人会不自然的问,这样的东西IE能支持吗?当我告诉大家@font-face这个功能早在IE4就支持了你肯定会感到惊讶.我的Blog就使用了许多这样的自定义Web字体,比如说首页的Logo,Tags以及页面中的手写英文体,很多朋友问我如何使用,能让自己的页面也支持这样的自定义字体,一句话这些都是@fo