getActivity()和Fragment中的视图之间的区别 [英] Difference between getActivity() and view in Fragment

查看:89
本文介绍了getActivity()和Fragment中的视图之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

getActivity().findViewById(...)

View view = inflater.inflate(R.layout.fragment_fragment_v, null);
view.findViewById(...)

在片段中(将活动"转换为片段时)?

in Fragment (when converting Activity to Fragment)?

推荐答案

区别在于,使用getActivty.findViewById(...)可以在活动范围(活动布局)中找到视图.使用iflater.inflate(R.layout.fragment_fragment_v, null); view.findViewById(...),您可以扩大片段的布局,然后在该布局中查找视图.

The difference is that with getActivty.findViewById(...) you are finding views in the scope of activity (activity's layout). With iflater.inflate(R.layout.fragment_fragment_v, null); view.findViewById(...) you are inflating the layout of your fragment and then finding view's in that layout.

但是由于您的片段已附加到活动中,因此您会同时找到该视图,但是我建议您在片段范围内找到该片段的视图,因为可能有多个具有相同布局的片段,这意味着可能会有多个视图与相同的ID相关联,从而导致getActivity().findBiewById(...)方法不可靠

But since your fragment is attached to the activity, you will find the view bothways, but I suggest you are finding view's for your fragment in your fragment's scope since there may be several fragments that have common layouts meaning there may be several view's associated with the same ID and that makes the getActivity().findBiewById(...) method unreliable

这篇关于getActivity()和Fragment中的视图之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆