How to add a view programmatically to RelativeLayout?

后端 未结 2 2086
梦谈多话
梦谈多话 2020-12-01 04:03

Can you give me a very simple example of adding child view programmatically to RelativeLayout at a given position?

For example, to reflect the followin

2条回答
  •  广开言路
    2020-12-01 04:45

    Heres an example to get you started, fill in the rest as applicable:

    TextView tv = new TextView(mContext);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    params.leftMargin = 107
    ...
    mRelativeLayout.addView(tv, params);
    

    The docs for RelativeLayout.LayoutParams and the constructors are here

提交回复
热议问题