LinearLayout layout = (LinearLayout) mInflater.inflate( R.layout.cell_check_item, null); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( UPDeviceInfo.getDeviceWidth(), LayoutParams.WRAP_CONTENT); rootLayout.addView(convertView, param);
我希望手動設置layout的寬度,結果就報錯(雖然很小的幾率報錯)
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
解決方法:換成從原來的view中獲取LayoutParams,而不是新建一個LayoutParams
LinearLayout.LayoutParams param = (android.widget.LinearLayout.LayoutParams) checkLayout
.getLayoutParams();
param.width = UPDeviceInfo.getDeviceWidth();
看來,當一個View已經有了LayoutParams,最好不要添加一個新的LayoutParams。