该问题网上比较多的答案,都是要求修改inflate条目布局的方式,具体如下:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ble_result_item_unmatched, parent, false);
关键在于parent不能传空.
假如使用
LayoutInflater.from(parent.getContext()).inflate(R.layout.ble_result_item_unmatched, null, false);
则很可能出现条目无法横向铺满的情况,即便你在view的布局中设置了layout_width为match_parent,原因在于如果parent(即父布局)传空,则方法内部实现的时候,会忽略你设置的layout参数,
parent不传空,attachToRoot传false:
if (root != null) {
// 系统根据父布局生成layoutParams
params = root.generateLayoutParams(attrs);
// 如果不添加到父布局,则添加layoutParams
if (!attachToRoot) {
temp.setLayoutParams(params);
}
}
parent不传空,attachToRoot传true:
// 如果父布局不空,且添加到父布局
if (root != null && attachToRoot) {
root.addView(temp, params);
}
parent传空,则只是inflate布局,但并不会添加layout参数.
如果使用:
View view = View.inflate(parent.getContext(), R.layout.ble_result_item_unmatched, null);
该方法内部是用的也是该方法:
LayoutInflater.inflate(resource, root, root != null)