最近项目抽取一个公共的父布局时,使用inflate动态加载头布局时,出现xml"最外层的属性" 不生效的情况,解决办法如下:
解决办法:
1、设置rootView,attachToRoot默认为true
((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.navigation_layout, linearLayoutRoot);
2、设置rootView,attachToRoot修改为false
View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.navigation_layout, linearLayoutRoot);
linearLayoutRoot.addView(view );
具体解决过程如下:
一、问题背景
使用LayoutInflater类的inflate()方法:
首先,解释一下方法的参数,resource是需要加载的布局文件R.layout.xx,ViewGroup root是一个父布局,boolean attachToRoot是否添加加载的布局到父布局里面。
使用时,遇到加载的布局文件的 “最外层的属性” 不生效的情况,例:加载如下布局:
加载代码如下:
会出现下面第一种不生效的情况:
二、解决过程
LinearLayout linearLayoutRoot = new LinearLayout(this);
linearLayoutRoot.setOrientation(LinearLayout.VERTICAL);
View headView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.navigation_layout, null);
以上加载方式,导致头布局最外层效果不生效。
于是查找资料,定位inflate参数的问题,修改如下:
LinearLayout linearLayoutRoot = new LinearLayout(this);
linearLayoutRoot.setOrientation(LinearLayout.VERTICAL);
View headView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.navigation_layout, linearLayoutRoot);
linearLayoutRoot.addView(headView );
但是发生如下错误:
分析inflate源码,发现如上设置时,系统已经执行了addView():
public View inflate(XmlPullParser parser, @Nullable ViewGroup root,
boolean attachToRoot) {
synchronized (mConstructorArgs) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, “inflate”);final Context inflaterContext = mContext; final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; View result = root; //第9行 try { // Look for the root node. int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!"); } final String name = parser.getName(); if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException("
can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, inflaterContext, attrs, false); } else { // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, inflaterContext, attrs);//第42行 ViewGroup.LayoutParams params = null; if (root != null) { //第46行 if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } } //第58行 if (DEBUG) { System.out.println("-----> start inflating children"); } // Inflate all children under temp against its context. rInflateChildren(parser, temp, attrs, true); if (DEBUG) { System.out.println("-----> done inflating children"); } // We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { //第73行 root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. if (root == null || !attachToRoot) { //第79行 result = temp; } } } catch (XmlPullParserException e) { final InflateException ie = new InflateException(e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } catch (Exception e) { final InflateException ie = new InflateException(parser.getPositionDescription() + ": " + e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } finally { // Don't retain static reference on context. mConstructorArgs[0] = lastContext; mConstructorArgs[1] = null; Trace.traceEnd(Trace.TRACE_TAG_VIEW); } return result; } }
第一种情况:
在第9行可以看到,系统把我们传入的父布局root赋值给了变量result,
继续往下看到第42行,创建了一个View temp,这个就是根据我们的布局文件view_button.xml创建的View(以下temp都指这个),
重点来了:
再看到第73-75行,第二种情况:
当root!= null,并且attachToRoot == true时,系统会自动帮你把加载的布局添加到root中,此时 addView是带有52行生成的LayoutParams参数的,所以属性也会生效。
最后看79-81行,第三种情况:
只要root == null或者attachToRoot == false,系统就会把temp赋值给变量result,最后返回的result其实就是temp对象,否则root != null并且attachToRoot == true返回的就是前面第9行赋值的父布局root对象。