Android 代码动态设置margin(报错:ViewGroup$MarginLayoutParams cannot be cast to )

时间:2024-10-09 07:16:36
 params = ();
 marginParams = null;
//获取view的margin设置参数
    if (params instanceof ) {
        marginParams = () params;
    } else {
//不存在时创建一个新的参数
//基于View本身原有的布局参数对象
marginParams = new (params);
    }
(0,0,1,0);

 

千万记住这里面的if 语句的判断, 如果直接marginParams = new (params); 会报错

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: , PID: 14673
  3. : $MarginLayoutParams cannot be cast to $LayoutParams
  4. at (:779)
  5. at (:685)
  6. at (:22093)
  7. at (:6614)
  8. at (:1514)
  9. at (:806)
  10. at (:685)
  11. at (:22093)
  12. at (:6614)