您的位置:首页 > 移动开发 > Android开发

Android中字体设置Font

2015-07-08 14:03 393 查看
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<root.getChildCount();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);
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: