Android 全局设置字体

说到这,我估计你心理已经有全新的解决方案了,那就是利用setFactory,相关代码如下(在BaseActivity中):

publicstaticTypeface typeface;

@Override

protected void onCreate(Bundle savedInstanceState){

if(typeface ==null)   

 {       

 typeface = Typeface.createFromAsset(getAssets(),"hwxk.ttf");  

  }    

LayoutInflaterCompat.setFactory(LayoutInflater.from(this),newLayoutInflaterFactory()    {

@Override

public View onCreateView(View parent, String name, Context context, AttributeSet attrs)        {           

AppCompatDelegate delegate = getDelegate();           

View view = delegate.createView(parent, name, context, attrs);

if(  view!=null&& (viewinstanceofTextView))           

{                ((TextView) view).setTypeface(typeface);           

}

return view;       

}    });

super.onCreate(savedInstanceState);   

setContentView(R.layout.activity_main);}

你可能感兴趣的:(Android 全局设置字体)