android - 如何在Android中动态设置边距?

标签 android margins

我目前正在做一个包含自定义警报对话框的 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.LayoutParamsRelativeLayout.LayoutParams

关于android - 如何在Android中动态设置边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062187/

相关文章:

ios - 如何创建带边距、圆角和阴影的 UITableView?

html - margin-bottom 不适用于 div

html - 使用 CSS Floats 和边距问题

android - 操作栏上的下拉菜单

Android Studio 永远无法下载 fastutil-7.2.0.jar

android - 如何在社交身份验证 android 中获取和重用访问 token ?

android - 是否可以将 SeekBar 的拇指图像延伸到栏外?

HTML 颜色和边距呈现不同(相同的代码、CSS 和浏览器)

java - Android:如何使用加速度计计算设备掉落的高度?

java - 从类更改 Activity 中的对象