Android开发FrameLayout动态添加控件位置问题

来源:互联网 发布:淘宝创建店铺流程 编辑:程序博客网 时间:2024/05/16 15:12

       首先FrameLayout的子控件默认都是从左上角显示的,还有最后添加的在最前面显示。

        一般是对FrameLayout.LayoutParams params进行操作,刚开始一直params.rightMargin来定位置,访了第一条准则,那就换种思路来做,比如params.leftMargin

        第二个重要的点:params需要每次生成一个新的,否则它以之后改变的为准的

示例代码:

for(int i = 0;i < openBean.getList().size();i++){                    //需要每次new Params,防止用的都是最后一个的设置                    FrameLayout.LayoutParams headParams = new FrameLayout.LayoutParams(Utils.dip2px(mContext,40),Utils.dip2px(mContext,40));                    View headGroupView = UIUtils.inflate(mContext,R.layout.circle_head_layout);                    RelativeLayout rlHead = (RelativeLayout)headGroupView.findViewById(R.id.rl_circle_head);                    ImageView headImage = (ImageView)headGroupView.findViewById(R.id.iv_head_small);                    headParams.setMargins(i*Utils.dip2px(mContext,30),0,0,0);                    headGroupView.setLayoutParams(headParams);                    ImageLoaderUtil.loadCircular(headImage, openBean.getList().get(i).getHead_img(), mContext);                    holder1.fl_recommend_open_user.addView(headGroupView);                }