Typeface字体类

一、Typeface类位于android.graphics包下,用于设置文本的字体

常用字体:
Typeface.DEFAULT :缺省字体
Typeface.DEFAULT_BOLD:缺省加粗
Typeface.MONOSPACE
Typeface.SANS_SERIF
Typeface.SERIF

二、使用createFromAsset()方法进行加载

createFromAsset(AssetManager mgr,String path);
  1. 作用:根据指定路径下的字体文件创建字体;
  • mgr:项目的assets文件夹资源的管理器;
  • path:字体文件所在路径,包括字体文件名;


    Typeface字体类_第1张图片
    asset_typeface.png
  1. 程序效果图:
Typeface字体类_第2张图片
字体效果图.png
public class TypefaceActivity extends AppCompatActivity {
    TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        mTextView= (TextView) findViewById(R.id.textView);
    }
    public void onClick(View view){
        Typeface typeface=null;
        switch (view.getId()){
            case R.id.btnDefaultBold:
                typeface=Typeface.DEFAULT_BOLD;
                break;
            case R.id.btnFzgl:
                typeface=Typeface.createFromAsset(getAssets(),"fzcy.ttf");
                break;
            case R.id.btnFzcy:
                typeface=Typeface.createFromAsset(getAssets(),"fzgl.ttf");
                break;
            case R.id.btnFzhl:
                typeface=Typeface.createFromAsset(getAssets(),"fzhl.ttf");
                break;
            case R.id.btnPop:
                typeface=Typeface.createFromAsset(getAssets(),"pop.ttf");
                break;
            case R.id.btnVIVALDII:
                typeface=Typeface.createFromAsset(getAssets(),"VIVALDII.TTF");
                break;
        }
        mTextView.setTypeface(typeface);
    }
}


    

你可能感兴趣的:(Typeface字体类)