Cómo configurar el margen dinámicamente en Android?


Actualmente estoy haciendo una aplicación para Android que contiene el diálogo personalizar alerta. Contiene un botón , pero no puedo establecer el margen para el botón . el código se da a continuación. el método setmargin no funciona

AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);

button.setText("Send");
LayoutParams buttonLayoutParams 
    = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

button.setLayoutParams(buttonLayoutParams);

resetPassword=editText.getText().toString();

LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);

myDialog.setView(layout);
Author: AndyN, 2012-06-16

4 answers

Escriba el Siguiente código para establecer el Margen, puede ayudarlo.

AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);
EditText editText = new EditText(Login.this);
TextView textView = new TextView(Login.this);
button.setText("Send");
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutParams.setMargins(50, 10, 0, 0);
button.setLayoutParams(buttonLayoutParams);
String resetPassword = editText.getText().toString();
LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);
myDialog.setView(layout);
myDialog.show();

Use LinearLayout.LayoutParams o RelativeLayout.LayoutParams de acuerdo con la disposición principal de la vista secundaria

 49
Author: Dipak Keshariya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-02 08:56:18
buttonLayoutParams.bottomMargin
buttonLayoutParams.topMargin
buttonLayoutParams.leftMargin
buttonLayoutParams.rightMargin

Se puede utilizar para establecer márgenes

 3
Author: user1458071,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-16 09:18:27

El método setMargin() está disponible si estás usando LinearLayout.LayoutParams pero no si estás usando ViewGroup.LayoutParams. Dipak Keshariya alude a esto pero no lo dice con tantas palabras.

 2
Author: Klepto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-08 16:49:58
You can set in LinearLayout margin

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);
 1
Author: Ajay Keshri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-02 09:37:42