今天在写 RecyclerView 的时候,item 布局无论怎么改,match_parent 都失效。在 AS 中预览并没有问题,但是运行之后显示的效果为 wrap_content。很是奇怪,难道是 java 代码里面动态修改了?检查了一番,并没有。
后来在网上查了一下,说是 inflate 时候有问题,解决方案如下:
View view = View.inflate(mContext, R.layout.item_comment_time_line, null);
需要将其改为:
//初始化
mInflater = LayoutInflater.from(mContext);
//onCreateViewHolder
View view = mInflater.inflate(R.layout.item_comment_time_line, parent, false);
改正后item布局的match_parent属性就起作用了。