1. 背景
函数
给最外层layout设置固定宽高,而后使用inflate(layoutId, null )方式加载,则layoutId的最外层的控件的宽高是没有效果的。3d
2. 缘由分析xml
(1)缘由概述blog
layout_width,layout_height是相对父View设置的,在没有指定父view的状况下inflate,没有生成对应的LayoutParams。im
(2)代码分析layout
inflate对外主要有两种函数实现:db
public View inflate(int resource, ViewGroup root);img
public View inflate(int resource, ViewGroup root, boolean attachToRoot);view
这两个函数最终都会调用public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)函数,在此函数中有如下代码:vi
能够看出,只有在root!=null的状况下才会根据xml参数(attrs)建立并设置LayoutParams,此时设置的宽高高才会起效。
3. 解决方案
(1)在inflate的时候指定父view
(2)对于inflate的时候没法指定父view的状况,须要以后在将此view添加到父view的时候从新利用LayoutParams设置宽高。