Help in spacing between EditText

2

I can not give a space between EditTexts in my project. the amount of EditTExts is generated according to the amount entered by the user, that is, it has been programmed in the Java Code. : //i.stack.imgur.com/lfOAW.png ">

    
asked by anonymous 29.10.2017 / 22:13

2 answers

2

Since you already use LayoutParams when adding the new view , it's simpler to explain.

Before calling layout.addView there on the last line, you must declare LayoutParams before (as you already do in layout.addView , but must be before you can define margins), shortly after you define its margins that you want your editText to have) and then just play it by adding 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 also another way to set layoutParams of your view , the last line of code above you could change 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 ok.

    
30.10.2017 / 00:22
1
if (p instanceof LinearLayout.LayoutParams) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)p;
        if (_default) lp.setMargins(mc.oml, mc.omt, mc.omr, mc.omb);
        else lp.setMargins(mc.ml, mc.mt, mc.mr, mc.mb);
        this.setLayoutParams(lp);
    }

Because this.getLayoutParams (); returns a ViewGroup.LayoutParams, which has no topMargin attributes, bottomMargin, leftMargin, RightMargin. The mc instance you see is just a MarginContainer that contains offset margins (-3dp) and (oml, omr, omt, omb) and the original margins (ml, mr, mt, mb).

Ref: link

    
29.10.2017 / 23:43