今天在寫demo的是時(shí)候遇到一個(gè)奇怪的問題脖含,recycleview的item寬度始終無法全屏笋妥。
查了一下相關(guān)資料,大部分都解決方法是修改onCreateViewHolder里面的view初始化方法如:
View view= LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
但是我這里明明是這么寫的啊~
這時(shí)我開始懷疑是不是容器導(dǎo)致的問題,我把ConstraintLayout換成線性布局消恍,妥妥的可以寬度可以全屏煎楣,看來是ConstraintLayout和Recycleview 組合使用的時(shí)候會(huì)有這個(gè)問題豺总。
我把recycleview的layout_width從match_parent改為0dp,問題解決了择懂。
查了一下相關(guān)文檔喻喳,發(fā)現(xiàn)
ConstraintLayout 并不支持match_parent
1.直接包含在ConstraintLayout下面的view是不支持match_parent的,使用0dp替換困曙。
2.嵌在直接包含在ConstraintLayout下面的view里面的view是支持match_parent的表伦,隨你怎么用。
相關(guān)文檔:https://stackoverflow.com/questions/37603751/set-width-to-match-constraints-in-constraintlayout
在這里做個(gè)備忘吧