UIAndroid开发Android知识

Typeface字体类

2017-01-07  本文已影响770人  dayang

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

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

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

createFromAsset(AssetManager mgr,String path);
  1. 作用:根据指定路径下的字体文件创建字体;
  1. 程序效果图:
字体效果图.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);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/btnDefaultBold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="默认加粗字体"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btnFzcy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="方正粗圆"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btnFzgl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="方正古隶"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btnFzhl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="方正华隶"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btnPop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="pop"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btnVIVALDII"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="VIVALDII"
        android:onClick="onClick"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello 安卓!"
        android:typeface="serif"
        android:textSize="20sp"/>
</LinearLayout>
上一篇 下一篇

猜你喜欢

热点阅读