I tried to programmatically add a new relativelayout after another relativelayout without overlap.
LinearLayout ll = (LinearLayout)findViewById(R.id.mainLL);
ll.setOrientation(LinearLayout.VERTICAL);
RelativeLayout rl = new RelativeLayout(MainActivity.this);
rl.setId(2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 50);//RelativeLayout.LayoutParams.WRAP_CONTENT
ll.addView(rl, lp);
TextView iTextView = new TextView(this);
iTextView.setTextSize(pthTextFontSize);
iTextView.setText("ok");
rl.addView(iTextView);
// finish adding the first relativelayout.
RelativeLayout rl2 = new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 50);
lp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp2.addRule(RelativeLayout.BELOW, rl.getId());
rl2.setLayoutParams(lp2);
ll.addView(rl2);
TextView iTextView2 = new TextView(this);
iTextView2.setTextSize(pthTextFontSize);
iTextView2.setText("abc");
rl.addView(iTextView2);
However, the word "ok" and "abc" are overlapped. Can anyone help me to check what is wrong? Thank a lot for help.
In short: There is a linearlayout(occupied the full screen). Then I try to add two linearlayouts.The parent of relative layout "rl2" is "ll", and i tried to put "rl2" at ALIGN_PARENT_LEFT of "ll", and below "rl1", but they overlap.
I didn't test your code but seeing the logic, the problem might be because you have used ALIGN_PARENT_LEFT for Relative Layout 2. Just to explain briefly, the parent of relative layout "rl2" is rl1. So, the text "abc" ( in rl2) is being aligned in the rl1's leftmost part, where "ok" exists. That's why they overlap. I haven't tried it myself but try using:
lp2.addRule(RelativeLayout.ALIGN_LEFT);
Tell me if it helps. All the best
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.