2
How do you already use the LayoutParams
when adding the new view
, is simpler to explain.
Before calling the layout.addView
there on the last line, you must declare the Layoutparams before (as you are already doing in the layout.addView
, but it needs to be before to be able to define the margins), right after you define the margins of it (you want your editText
have) and then just play it while adding to view
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
EditText[] editT = new EditText[6];
for(int i=0; i < 6; i++){
editT[i] = new EditText(MainActivity.this);
editT[i].setHint("Periodo" + (i+1));
editT[i].setGravity(Gravity.CENTER);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 100); //LinearLayout ou o tipo do seu ViewGroup
layoutParams.setMargins(0,15,0,0); //Define as margins em left, top, right e bottom respectivamente
ll.addView(editT[i], layoutParams); //Adiciona a view e informa o LayoutParams que ela irá usar (que contém as margens e o tamanho da view)
}
There is another way to define the layoutParams
of his view
, the last line of the above code you could exchange for these:
editT[i].setLayoutParams(layoutParams); //Define o LayoutParams da view
ll.addView(editT[i]); //Adiciona somente a view, já que já foi definido o LayoutParams
I tested here in Android Studio now and it worked all ok.
If any answer has solved your problem it would be interesting to mark it as answered
– underfilho