案例分析
父控件InfobarLayout:
子控件InfobarView:
加载InfobarView:
实际效果:
有么有发现,并没有达到预期的width为match_parent而有种wrap_content感觉。
为啥尼?带着疑问翻开度娘。
代码逻辑走到3720行:
LayoutParams params = child.getLayoutParams();
params是空的。
走到逻辑3722行:
params = generateDefaultLayoutParams();
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
造成子控件显示效果为wrap_content效果。
又度娘了下Inflater
回到加载InfobarView:
View view = inflater.inflate(R.layout.cable_infobarview, null);
Android studio有个不友好的提示:
Avoid passing `null` as the view root (needed to resolve layout parameters on the inflated layout's root element)
本文分析了InfobarView在父控件InfobarLayout中显示宽度不符合预期的问题。通过源码跟踪发现,子控件默认使用了wrap_content布局参数,而非match_parent。文章提供了修复方案并介绍了Android LayoutInflater的相关注意事项。

1922

被折叠的 条评论
为什么被折叠?



