1. 程式人生 > >Android動態新增控制元件約束位置

Android動態新增控制元件約束位置

用LayoutParams:
 
RelativeLayout insertLayout = (RelativeLayout)view1.findViewById(R.id.screen);//screen是一個RelativeLayout 佈局的id

ImageView imgApple2 = new ImageView(MainActivity.this);
				        imgApple2.setBackgroundColor(Color.parseColor("#ffb6b4"));

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
        layoutParams.topMargin=8;
        layoutParams.leftMargin=8;
        layoutParams.rightMargin=8;
        layoutParams.bottomMargin=8;

insertLayout.addView(imgApple2,layoutParams);