Android Fragment中获取上下文

2021-06-15  本文已影响0人  浅_若清风
在Fragment类或其子类中,LayoutInflater.from加载xml布局,我们不能直接使用LayoutInflater.from(this),否则报错。 img_1.png
点击from进入可以看到,from需要传入上下文Context类型 img_2.png
我们可以如下定义
final Context context = getActivity();
LayoutInflater.from(context).inflate(R.layout.xxx,root:null);

或者直接使用this.getActivity()来表示

LayoutInflater.from(this.getActivity()).inflate(R.layout.xxx,root:null);
上一篇 下一篇

猜你喜欢

热点阅读