动态添加布局

RelativeLayout rl =newRelativeLayout(this);

TextView btn1 =newTextView(this);

btn1.setText("erteryrtyrrturtu");

btn1.setId(1);

RelativeLayout.LayoutParams lp1 =newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

lp1.topMargin= FeViewUtils.dpToPx(10);

lp1.leftMargin= FeViewUtils.dpToPx(15);

// btn1位于父View的顶部,在父View中水平居中

rl.addView(btn1,lp1);

TextView btn2 =newTextView(this);

btn2.setText("wegdfgdfharfhfdhad");

btn2.setId(2);

RelativeLayout.LayoutParams lp2 =newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

lp2.topMargin= FeViewUtils.dpToPx(10);

lp2.addRule(RelativeLayout.BELOW,1);

lp2.addRule(RelativeLayout.ALIGN_LEFT,1);

// btn2位于btn1的下方、其左边和btn1的左边对齐

rl.addView(btn2,lp2);

TextView btn3 =newTextView(this);

btn3.setText("ertertwertertewr|");

btn3.setId(3);

RelativeLayout.LayoutParams lp3 =newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

lp3.topMargin= FeViewUtils.dpToPx(10);

lp3.addRule(RelativeLayout.BELOW,2);

lp3.addRule(RelativeLayout.ALIGN_LEFT,1);

// btn3位于btn1的下方、btn2的右方且其右边和btn1的右边对齐(要扩充)

rl.addView(btn3,lp3);

你可能感兴趣的:(动态添加布局)