获得 LayoutInflater 实例的三种方式
1.LayoutInflater infalte=context.getSytemService(Context.LAYOUT_INFLATE_SERVICE);
2.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
3.LayoutInflater flater = LayoutInflater.from(this);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_menu, null);//正确
view = inflater.inflate(R.layout.layout_menu, container, false);//正确
view = View.inflate(getActivity(), R.layout.layout_menu, null);//正确
view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_menu, null);//正确,跟参数LayoutInflater inflater一样
view = inflater.inflate(R.layout.layout_menu, container);//错误
view = inflater.inflate(R.layout.layout_menu, container, true);//错误
view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_menu, container);//错误
return view;
}
ViewGroup.LayoutParams仅有width和height2个属性,也就是说会忽略掉inflate出来的xml的根layout的除了width和height以外的其他所有属性
所以,如果要给根layout设置margin,可以在这个根layout外再放一个RelativeLayout(使他不再处于根位置)
或者换成padding去解决,因为padding不是LayoutParams中的属性