动态的添加ImageView并居中显示

来源:互联网 发布:开源搜索引擎源码 编辑:程序博客网 时间:2024/06/06 02:15

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