RecycylerView中itemview布局宽高无效问题

177 阅读1分钟

itemview宽高失效问题

无论itemview的根布局是何种viewGroup总是会展示成wrap_content的样式
//例外:当布局是relativeLayout时,偶尔可能正确,可能是其特殊的onMessure()方法导致的

失效写法

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_test_item,null)
//原因简介:没有指定父布局的情况下,item的LayoutParams由Recyclerview默认生成wrap_content模式

正确写法

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_test_item,parent,false)
//原因简介:attachToRoot 为 false,因此,加载的View不会添加到root,但是会用root生成的LayoutParams信息

相关介绍:www.jianshu.com/p/9a6db88b8…