报错日志:

 报错代码:

场景:调整左右边距时报错

 LinearLayout.LayoutParams lp =
                        (LinearLayout.LayoutParams) lightNavStartNavFocusArea.getLayoutParams();
                lp.leftMargin = leftMargin;
                lightNavStartNavFocusArea.setLayoutParams(lp);

解决方法:使用ViewGroup.MarginLayoutParams 代替 LinearLayout.LayoutParams

ViewGroup.MarginLayoutParams lp =
                        (ViewGroup.MarginLayoutParams) lightNavStartNavFocusArea.getLayoutParams();
                lp.leftMargin = leftMargin;
                lightNavStartNavFocusArea.setLayoutParams(lp);

使用该原因:该场景主要用于调整左右margin边距

而不管是线性布局,还是相对布局都继承自ViewGroup.MarginLayoutParams

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐