I have relativelayout as container, there i have set text and image, next i addView() this relativelayout twice to one linear layout, everything is fine but i cant separate the two relativelayouts they are alWays stick one to another, here is the code:
LinearLayout
layout = new LinearLayout(context);
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setMargins(5, 5, 5, 5);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setPadding(5, 5, 5, 5);
layout.setLayoutParams(ll);
layout.setId(i);
layout.addView(m1.box);
layout.addView(m2.box);
where m1 and m2 are:
RelativeLayout:
box = new RelativeLayout(context);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rl.setMargins(5, 5, 5, 5);
rl.width = 125;
rl.height = 125;
box.setId(i);
box.setPadding(5, 5, 5, 5);
box.setBackgroundColor(color1);
box.setLayoutParams(rl);
//add some text and image
How to separate m1 from m2? Now they look like OO i need to add space O_O.
First, this doesn’t work because the parent layout is a
LinearLayout, and theLayoutParamsyou defined are ofRelativeLayout. theLayoutParamsshould match the type of the parent and not the child, because they tell the parent how and where to place it’s children.Second, use
addView(View v, LayoutParams lp)instead ofaddView(View v)to prevent this kind of mistakes.