一廷没、Typeface類位于android.graphics包下糙麦,用于設(shè)置文本的字體
常用字體:
Typeface.DEFAULT :缺省字體
Typeface.DEFAULT_BOLD:缺省加粗
Typeface.MONOSPACE
Typeface.SANS_SERIF
Typeface.SERIF
二歇攻、使用createFromAsset()方法進(jìn)行加載
createFromAsset(AssetManager mgr,String path);
- 作用:根據(jù)指定路徑下的字體文件創(chuàng)建字體贷盲;
- mgr:項(xiàng)目的assets文件夾資源的管理器坏匪;
-
path:字體文件所在路徑拟逮,包括字體文件名;
- 程序效果圖:
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="默認(rèn)加粗字體"
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>