此处如果传null,lint 会有警告,
一般如果用View.inflate(),警告会解除,但事实是被suppresses the warning,
根本原因是:
如果传null,相当于告诉LayoutInflater 你不知道这个view的父母是谁,而如果运气好的话(一般情况下都是),该view 的默认的参数和xml里写的参数相同,但如果不同,xml里的根布局属性将丢失;
常见的错误写法:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflate(R.layout.item_row, null);
}
return convertView;
}
此时item_row里的根布局属性将丢失;
应为:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflate(R.layout.item_row, parent, false);
}
return convertView;
}
但是如果是自定义AlertDialog,在渲染自定义xml时候,也是传null,但是此时,因为AlertDialog并没有其rootView(它不知道何时渲染,只有show方法),并且 无论你的LayoutParams 设成什么,AlertDialog 都会无视掉并且设置成 match_parent,
也就是说 ,此时 你只能传null, 否则就会crash
参考链接:https://possiblemobile.com/2013/05/layout-inflation-as-intended/