Edittext Spacing Help

Asked

Viewed 397 times

2

I can’t get a hold of Edittexts on my project. the amount of Edittexts are generated according to the amount informed by the user, that is, it was programmed in the Java Code.inserir a descrição da imagem aqui

  • If any answer has solved your problem it would be interesting to mark it as answered

2 answers

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.

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 does not have topMargin, bottomMargin, leftMargin, Rightmargin attributes. The mc instance you see is just a Margincontainer that contains both the compensation margins (-3dp) and (oml, Omr, omt, Omb) and the original margins (ml, mr, mt, Mb).

Ref.: https://stackoverflow.com/questions/12728255/in-android-how-do-i-set-margins-in-dp-programmatically

Browser other questions tagged

You are not signed in. Login or sign up in order to post.