2012-06-16 10 views
25

Ich mache derzeit eine Android-Anwendung, die anpassen Alarmdialogfeld enthält. Es enthält eine Schaltfläche, aber ich kann den Rand für die Schaltfläche nicht festlegen. Der Code ist unten angegeben. setmargin Methode funktioniert nichtWie wird die Marge in Android dynamisch festgelegt?

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); 

Antwort

44

Code über Nach Margin zu setzen, kann es Ihnen helfen.

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(); 

Verwendung LinearLayout.LayoutParams oder RelativeLayout.LayoutParams nach Eltern Layout der untergeordneten Ansicht

+0

es funktioniert nicht. der Knopf ist innerhalb alertdialog – user1057197

+0

Bitte sehen Sie meine bearbeitete Antwort, es funktioniert für mich. –

+1

Nein, es funktioniert nicht. In der Methode setMargin wird ein Fehler beim Hinzufügen von Cast zu buttonlayoutparams angezeigt. – user1057197

2
buttonLayoutParams.bottomMargin 
buttonLayoutParams.topMargin 
buttonLayoutParams.leftMargin 
buttonLayoutParams.rightMargin 

können Ränder eingestellt werden

+0

es funktioniert nicht – user1057197

+1

Legen Sie die Layoutparameter für Ihr 'LinearLayout' fest? – user1458071

2

Die setMargin() Methode ist verfügbar, wenn Sie LinearLayout.LayoutParams verwenden, aber nicht, wenn Sie ViewGroup.LayoutParams verwenden. Dipak Keshariya spielt darauf an, sagt es aber nicht in so vielen Worten.

0
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); 
Verwandte Themen