Insert marginRight into a TextView by Java code

2

I've made the following code to insert TextViews into a LinearLayout already defined.

public void inserirLacunas(){
    LinearLayout ll = (LinearLayout) findViewById(R.id.layoutLetras);

    for(int i = 0; i < palavraCerta.length(); i++){
        TextView lacuna = new TextView(this);
        lacuna.setText("_");
        lacuna.setTextSize(40);
        ll.addView(lacuna);
    }
}

My question is: how can I put a marginRight in the TextView loophole, so that at the time the code runs, do not get TextViews too much next to each other?

  

wordCerta is another variable created previously in the code, which does not matter in this doubt

    
asked by anonymous 17.08.2015 / 19:09

1 answer

3

You need to create an object of type LinearLayout.LayoutParams , specify the parameters and assign it to TextView :

public void inserirLacunas(){
    LinearLayout ll = (LinearLayout) findViewById(R.id.layoutLetras);

    for(int i = 0; i < palavraCerta.length(); i++){
        TextView lacuna = new TextView(this);
        lacuna.setText("_");
        lacuna.setTextSize(40);

        //Ciar parâmetros 
                LinearLayout.LayoutParams params = 
               new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
                                             LinearLayout.LayoutParams.WRAP_CONTENT);


        //Definir as margens
        params.setMargins(left, top, right, bottom)//Introduza os valores pretendidos.
        //Atribuir os parâmetros ao TextView
        lacuna.setLayoutParams(params);
        ll.addView(lacuna);
    }
}
    
17.08.2015 / 19:30