Edit: As the View
which must be above CheckBox
is generated dynamically, this solution using RelativeLayout.BELOW
does not meet and generates problems even adding a id
auto-generated. To each addView
the RelativeLayout
will need to reevaluate all rules, generating an "overhead" in the solution.
A simple solution would be to adopt a LinearLayout
with orientation="vertical"
, because to each item inserted the LinearLayout
puts it below the previous.
The only modification to the current code would be:
for (int i = 0; i < json.length(); i++) {
// Precisa gerar um LayoutParams para cada View, não recomendo reutilizar.
LinearLayout.LayoutParams ml = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// Margem opcional
ml.setMargins(0, 5, 0, 0);
CheckBox cb = new CheckBox(this);
cb.setLayoutParams(ml);
_linearLayoutCbIngredientes.addView(cb);
}
To add dynamic margin in a View
which is adding to the Layout, needs:
- Create the
LayoutParams
specific to the subclass of ViewGroup
that will add to View
. The Builder of LayoutParams
receives the layout_width
and layout_height
that the View
will have.
- Set values before calling
addView(View view)
or use the addView(View view, LayoutParams param)
.
An example:
RelativeLayout.LayoutParams ml = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// A classe RelativeLayout.LayoutParams extende MarginLayoutParams,
// verificado na documentacao: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
ml.setMargins(0, 5, 0, 0);
CheckBox cb = new CheckBox(this);
cb.setChecked(true);
// Ou
cb.setLayoutParams(ml);
_relativeLayout.addView(cb);
// Ou
_relativeLayout.addView(cb, ml);
As a suggestion, why not use a rule to position a View
below another?
Using the rule would be:
RelativeLayout.LayoutParams ml = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// A classe RelativeLayout.LayoutParams extende MarginLayoutParams,
// verificado na documentacao: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
// Adiciona uma regra para o CheckBox ficar abaixo da View existente cujo id seja viewAcima.
// Parto do pressuposto que essa View ja esta no layout como mostrado na imagem.
ml.addRule(RelativeLayout.BELOW, R.id.viewAcima);
CheckBox cb = new CheckBox(this);
cb.setChecked(true);
// Ou
cb.setLayoutParams(ml);
_relativeLayout.addView(cb);
// Ou
_relativeLayout.addView(cb, ml);
In the latter case, the RelativeLayout
will add one more restriction in your CheckBox
should fall below the viewAcima
.
I believe this form is more correct, because using a fixed margin can generate problems depending on the density and screen size and etc of the device. Therefore, it may be that in some devices the fixed margin given is not sufficient to place the CheckBox
below the other View
.
Exception, create a
MarginLayoutParams
and puttopMargin
in it. TheLayoutParams
will depend on what kind of fatherCheckBox
.– Wakim
what did you mean by
tipo
? If it isRelativeLayout
,LinearLayout
...?– Leonardo
The class
ViewGroup
has a classLayoutParams
only withwidth
andheight
. Every subclass (RelativeLayout
,LinearLayout
etc) defines a classLayoutParams
who inherits fromViewGroup.LayoutParams
and some ofMarginLayoutParams
. The latter accepts margins, and also characterizes aViewGroup
that treats margins. The type would be which subclass is the father of theCheckBox
.– Wakim
@Wakim, I don’t know if I got it right, I added the
MarginLayoutParams
and now is not displayed theCheckBoxes
. I updated my question.– Leonardo