private void addViewByCode(){ View subView = new View(context); subView.setBackgroundColor(Color.RED); //subView的宽高 int subViewwidth = 700; int subViewHeight = 300; RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(subViewwidth, subViewHeight); //addRule默认是ALIGN_PARENT_LEFT+ALIGN_PARENT_TOP relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); int left = 300; int top = 200; relLayoutParams.setMargins(left, top, 0, 0); root_RelativeLayout.addView(subView, relLayoutParams); }
时间: 2024-10-05 04:22:36