动态设置padding值
1 2
| ImageView imageView=new ImageView(Contextcontext); imageView.setPadding(left,top,right,bottom);
|
动态设置margin值
1 2 3
| RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(10,10); params.setMargins(0, ScreenUtil.dip2px(5), 0, 0); view.setLayoutParams(params);
|
动态设置居中
1 2 3
| RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(10,10); params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); view.setLayoutParams(params);
|
同时也可以设置其他对齐方式
1 2 3
| lp.addRule(RelativeLayout.ALIGN_WITH_PARENT_TOP); lp.addRule(RelativeLayout.POSITION_BELOW, view1); lp.addRule(RelativeLayout.ALIGN_LEFT, view2);
|
动态设置宽高
1 2 3
| RelativeLayout.LayoutParams Params = (RelativeLayout.LayoutParams)mView.getLayoutParams(); Params.height = 100; mView.setLayoutParams(linearParams);
|
RelativeLayout.LayoutParams根据父布局的类型可以改为
RelativeLayout.LayoutParams、
LinearLayout.LayoutParams、
ViewGroup.LayoutParams
三选一
前Android/Vue开发,现Infra从业人员,主营监控/AWS