動態的新增ImageView到LinearLayout中並居中顯示

weixin_33724059發表於2018-04-23
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);

 

相關文章