Fragment的onCreate和onCreateView的区别
2个回答
展开全部
onCreate是指创建该fragment类似于Activity.onCreate,你可以在其中初始化除了view之外的东西,onCreateView是创建该fragment对应的视图,你必须在这里创建自己的视图并返回给调用者,例如
return inflater.inflate(R.layout.fragment_settings, container, false);。
super.onCreateView有没有调用都无所谓,因为super.onCreateView是直接返回null的。
return inflater.inflate(R.layout.fragment_settings, container, false);。
super.onCreateView有没有调用都无所谓,因为super.onCreateView是直接返回null的。
展开全部
1)onCreate是指创建该fragment,类似于Activity.onCreate,可以在其中初始化除了view之外的东西;
2)onCreateView是创建该fragment对应的视图,必须在这里创建自己的视图并返回给调用者。
例如
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main,container,false);
mLine1 = (TextView)rootView.findViewById(R.id.line1);
mLine2 = (TextView)rootView.findViewById(R.id.line2);
mDescription1 = (TextView)rootView.findViewById(R.id.line1_description);
mDescription2 = (TextView)rootView.findViewById(R.id.line2_description);
mVersion = (TextView)rootView.findViewById(R.id.version);
mSignWifi = (TextView)rootView.findViewById(R.id.advice);
mSignStreaming = (LinearLayout)rootView.findViewById(R.id.streaming);
mSignInformation = (LinearLayout)rootView.findViewById(R.id.information);
mPulseAnimation = AnimationUtils.loadAnimation(mApplication.getApplicationContext(), R.anim.pulse);
mTextBitrate = (TextView)rootView.findViewById(R.id.bitrate);
return rootView ;
}
2)onCreateView是创建该fragment对应的视图,必须在这里创建自己的视图并返回给调用者。
例如
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main,container,false);
mLine1 = (TextView)rootView.findViewById(R.id.line1);
mLine2 = (TextView)rootView.findViewById(R.id.line2);
mDescription1 = (TextView)rootView.findViewById(R.id.line1_description);
mDescription2 = (TextView)rootView.findViewById(R.id.line2_description);
mVersion = (TextView)rootView.findViewById(R.id.version);
mSignWifi = (TextView)rootView.findViewById(R.id.advice);
mSignStreaming = (LinearLayout)rootView.findViewById(R.id.streaming);
mSignInformation = (LinearLayout)rootView.findViewById(R.id.information);
mPulseAnimation = AnimationUtils.loadAnimation(mApplication.getApplicationContext(), R.anim.pulse);
mTextBitrate = (TextView)rootView.findViewById(R.id.bitrate);
return rootView ;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询