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);