1.一般使用默认字体,可以使用以下四种安卓系统提供的字体。
android:typeface="normal"
android:typeface="sans"
android:typeface="serif"
android:typeface="monospace"
2.如果需要设置其他的字体,则需要先得到字体的 ttf文件,例如 Arial.ttf文件等。
把ttf文件放在assets\fonts目录下。
public class FontTest {
public static void changeFont(ViewGroup root,Activity act){
Typeface tf = null;
//创建Typeface对象
tf = Typeface.createFromAsset(act.getAssets(), "fonts/Arial.ttf");
for(int i=0;i
View v = root.getChildAt(i);
if(v instanceof TextView){
((TextView)v).setTypeface(tf);
}
else if(v instanceof EditText){
((EditText)v).setTypeface(tf);
}
else if(v instanceof ViewGroup){
changeFont((ViewGroup)v, act);
}
}
}
}
public class MainActivity extends Activity {
LinearLayout rootview = null;
TextView world1 = null;
TextView world2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootview = (LinearLayout)findViewById(R.id.rootview);
FontTest.changeFont(rootview, this);
// Typeface tf = null;
// tf = Typeface.createFromAsset(this.getAssets(), "fonts/Arial.ttf");
// world1 = (TextView)findViewById(R.id.world1);
// world2 = (TextView)findViewById(R.id.world2);
// world2.setTypeface(tf);
}