动态的添加ImageView到LinearLayout中并居中显示

ImageView imageView = new ImageView(mActivity);
imageView.setImageResource(R.mipmap.gengduo);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
param.leftMargin = 40;
param.gravity = Gravity.CENTER;  //必须要加上这句,setMargins才会起作用,而且此句还必须在setMargins下面
imageView.setLayoutParams(param);

linearLayout.addView(imageView);

你可能感兴趣的:(动态添加控件)