1.基类需要继承AppCompatActivity
主要是为了调用getDelegate()拿到代理
2.要在Activity的super.onCreate(savedInstanceState)之前调用
3.具体替换代码如下:
override fun onCreate(savedInstanceState: Bundle?) {
//必须在super.onCreate之前调用
LayoutInflaterCompat.setFactory2(layoutInflater, object : LayoutInflater.Factory2 {
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
val view = delegate.createView(parent, name, context, attrs)
if (view is TextView) {
//这里可以修改为自己想要使用的字体
view.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
}
return view
}
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
return null
}
})
super.onCreate(savedInstanceState)
......
}