尝试将父布局更改为RelativeLayout
,并使用属性centerInParent
:
RelativeLayout rootLayout = new RelativeLayout(this);
rootLayout.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
ImageView ivOne = new ImageView(this);
ivOne.setImageResource(R.drawable.ic_launcher);
ivOne.setScaleType(ImageView.ScaleType.CENTER);
ivOne.setLayoutParams(params);
//TODO : add other views
rootLayout.addView(ivOne);