我目前正在做一个包含自定义警报对话框的 android 应用程序。它包含一个按钮,但我无法设置按钮的边距。代码如下。 setmargin 方法不起作用
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);
最佳答案
写下面的代码来设置边距,它可能对你有帮助。
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();
根据 subview 的父布局使用LinearLayout.LayoutParams
或RelativeLayout.LayoutParams
关于android - 如何在Android中动态设置边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062187/