用LayoutParams:可以设定控件的一些属性
RelativeLayout insertLayout = (RelativeLayout)view1.findViewById(R.id.lay);//lay是一个RelativeLayout 布局的id ImageView img = new ImageView(MainActivity.this);//新建一个ImageView控件 img.setBackgroundColor(Color.parseColor("#ffbbb4")); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100); layoutParams.topMargin=8; layoutParams.leftMargin=8; layoutParams.rightMargin=8; layoutParams.bottomMargin=8; insertLayout.addView(imgApple2,layoutParams);
时间: 2024-10-18 03:41:12