1. 程式人生 > >LinearLayout.setLayoutParams置動態設定的左右邊距報錯解決方案

LinearLayout.setLayoutParams置動態設定的左右邊距報錯解決方案

1.RelativeLayout 設定邊距使用此方法可以但是LinearLayout.setLayoutParams 就報錯型別轉換錯誤
RelativeLayout.LayoutParams hint_page_params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
hint_page_params.setMargins(setWith,0, setWith, 0);//設定左右邊距
hint_page_params.addRule(RelativeLayout.BELOW, 1); //設定位置

login_content_show_rl.setLayoutParams(hint_page_params);

2.LinearLayout.setLayoutParams  必須使用FrameLayout.LayoutParams設定引數

FrameLayout.LayoutParams  hint_page_params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
hint_page_params.setMargins(setWith,ActivityUtil.dip2px(context, 20), setWith, 0);//設定邊距
register_parent_rl.setLayoutParams(hint_page_params);