关于Inflate()、FragmentTransaction中replace、remove和add方法中的参数
一 inflater.inflate(R.layout.fragment_book_detail,container,false);
inflate()的用法是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并且显示功能。而setContentView()将布局设置成当前屏幕及Activity的内容,可以直接显示出来。
1 想要加载的layout的resource ID
2 加载的layout的父ViewGroup。传入container是很重要的,目的是为了让系统接受所要加载的layout的根view的layout参数,由它将挂靠的父View指定。
3 布尔值指示在加载期间,展开的layout是否应当附着在viewGroup.
二
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.replace(R.id.container,fragment);
动态替换或删除Fragment,这时需要FragmentTransaction的布局动态文件执行替换,参数1:父元素的id值 参数2:替换新的fragment对象
其中R.id.container只是res/layout/main.xml中定义的一个简单的容器,该容器只是用于盛装Fragment。
三
inflate 方法 通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

浙公网安备 33010602011771号