1. 程式人生 > >android 程式碼控制LinearLayout 寬度高度 報錯widget.LinearLayout$LayoutParams cannot be cast to android.widget...

android 程式碼控制LinearLayout 寬度高度 報錯widget.LinearLayout$LayoutParams cannot be cast to android.widget...

錯誤:程式碼修改LinearLayout寬度時,報錯java.lang.ClassCastException: android.widget.LinearLayoutLayoutParams cannot be cast to android.widget.RelativeLayoutLayoutParams
佈局:LinearLayout父佈局是RelativeLayout
因此,在程式碼裡修改LinearLayout寬度時不需要new,需要獲取它自己的LayoutParams

 ViewGroup.LayoutParams params = LinearLayout.getLayoutParams();
                    params.height = ViewGroup.LayoutParams.MATCH_PARENT;
                    LinearLayout.setLayoutParams(params);