addView导致子布局“match_parent”属性失效详解
2017-09-05 11:28
1296 查看
问题
项目中我们经常会使用inflater动态生成并加载
View,例如:
View v = inflater.inflate(R.layout.layout_child, null); parent.addView(v);
而在最后
addView的时候,也会经常遇到诡异的现象:
明明子布局我们设置的是
android:layout_width="match_parent",而最后真机的效果却是
"wrap_content",简直莫名其妙!
为什么会这样?
遇到这种诡异的bug,不妨先读读相关源码。
源码追踪
我们看下addView的源码。public void addView(View child) { addView(child, -1); } …… public void addView(View child, int index) { if (child == null) { throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); } LayoutParams params = child.getLayoutParams(); if (params == null) { params = generateDefaultLayoutParams(); if (params == null) { throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); } } addView(child, index, params); }
可以看到,我们直接调用
addView方法的时候,首先会获取子布局的参数:
LayoutParams params = child.getLayoutParams();
而这个方法的注解中有这样一段说明:
This method may return null if this View is not attached to a parent
ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
was not invoked successfully. When a View is attached to a parent
ViewGroup, this method must not return null.
当View没有挂载到父布局或者没有成功生成时,它的参数为null!
而刚好,我们前一步只是动态生成了
View,还没有挂到父布局,所以接下来,会调用默认参数
generateDefaultLayoutParams():
protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); }
看到了吧,默认参数是宽高都为
WRAP_CONTENT。
总结
当我们使用inflater生成子
View,然后直接调用
addView(View v)方法时,子布局的宽高设置都会失效!
解决方案
我们只需要在addView的时候,赋予子布局一个布局参数即可:parent.addView(v, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
相关文章推荐
- RecyclerView控件布局的"match_parent"属性失效
- RecyclerView控件列表项布局match_parent属性失效的根本原理
- RecyclerView中item布局的"match_parent"属性失效--LayoutInflate的深入了解
- RecyclerView控件列表项布局match_parent属性失效的根本原理
- Android Inflater addview导致加载子布局match_parent无效
- RecyclerView中的item的match_parent属性失效问题解决方案。
- 解决不论是layoutinflute还是listview的addView调用之后导致子布局的match失效
- ViewGroup.addView()后导致layout_height等属性失效原因
- RecyclerView中的item的match_parent属性失效问题解决方案
- RecyclerView的使用方法以及item布局"match_parent"属性失效
- RecycleView添加条目布局match_parent失效的问题
- 高级控件RecycleView 设置Match_Parent 失效的解决办法
- listview的子布局如果有HorizontalScrollView、CheckBox 、button,可能会导致onListItemClick点击事件失效。
- Activity中动态的add Fragment,发现fragment的根布局设置了,Match_parent,但是效果确是warp_content
- RecyclerView的列表布局中match_parent失效的解决方法
- textview使用SpanbleString导致ellsize的属性失效
- layout布局中的inflate导致属性失效的问题。
- Android设置布局参数,addView添加布局时指定控件属性
- android布局--Android fill_parent、wrap_content和match_parent的区别 三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精
- RecyclerView item中match_parent失效