zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android动态加入控件约束位置

Android 动态 控件 位置 约束 加入
2023-09-11 14:14:59 时间
用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);