親測解決:View.inflate動態添加子佈局時不能填滿問題

想要右圖的效果,填滿容器,可是實際上卻是左邊的效果,只需改一句代碼:

                  

1.原先我的代碼效果如上左圖,代碼如下:(佈局中使用了dataBinding)

 mBinding.clMeasurement.removeAllViews();// mBinding.clMeasurement是LinearLayout,用於裝子佈局的
 view = View.inflate(getActivity(), R.layout.item_high, null);//root爲null
//寫一些佈局的view.findViewById邏輯後,才寫下面這句
 mBinding.clMeasurement.addView(view);

 2.修改後的代碼,效果圖如右邊:

 mBinding.clMeasurement.removeAllViews();
 view = View.inflate(getActivity(), R.layout.item_high, mBinding.clMeasurement);//root爲容器佈局
//此處可寫一些佈局的view.findViewById邏輯

 

很簡單的解決方法,因爲當時腦子沒有轉過來,糾結了些時間也浪費了時間,在此記錄一下! 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章