动态的添加ImageView并居中显示

Android 中动态添加控件,并设置居中和Margin:
其中DisplayUtil.dip2px()是为了适配。

   SimpleDraweeView image = new SimpleDraweeView(getContext());
   LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   param.height = DisplayUtil.dip2px(getContext(), 140);
   param.width = DisplayUtil.dip2px(getContext(), 140);
   param.setMargins(0, DisplayUtil.dip2px(getContext(), 200), 0, 0); // Margin
   param.addRule(RelativeLayout.CENTER_HORIZONTAL);//水平居中
   addView(image, param);

猜你喜欢

转载自blog.csdn.net/sinat_24196195/article/details/77881115