android開發之修改全局自定義字體

有時候要用自定義字體的時候需要一個一個這樣設置非常的不方便,就大概寫了一下方便的方法,當然大家可以順便改一下里面的功能來適配自己的需求。代碼:

protected void changeFont(ViewGroup root) {       
		Typeface tf = Typeface.createFromAsset(getAssets(),
		"ITCAvantGardeStd-Bk.otf");    
        for(int i = 0; i <root.getChildCount(); i++) {
                 View v = root.getChildAt(i);
                 if(v instanceof TextView ) {
                         ((TextView)v).setTypeface(tf);
                         ((TextView)v).setTextSize(15);
                         ((TextView)v).setTextColor(Color.GRAY);
                 } else if(v instanceof Button) {
                         ((Button)v).setTypeface(tf);
                         ((Button)v).setTextSize(15);
                         ((Button)v).setTextColor(Color.GRAY);
                 } else if(v instanceof EditText) {
                         ((EditText)v).setTypeface(tf);
                         ((EditText)v).setTextSize(15);
                         ((EditText)v).setTextColor(Color.GRAY);
                 } else if(v instanceof ViewGroup) {
                         changeFont((ViewGroup)v);
                 }
         }
         
     }

順便附上獲取本activity下的獲取最外層控件代碼:

(ViewGroup) this.getWindow().getDecorView()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章