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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11062187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:51:34  来源:igfitidea点击:

How to set margin dynamically in Android?

androidmargins

提问by user1057197

I am currently doing an android application that contains customize alert dialog. It contains a button , but i can't set the margin for the button . the code is given below. setmargin method is not working

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

回答by Dipak Keshariya

Write below code to set margin, it may help you.

写下面的代码来设置边距,它可能对你有帮助。

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.LayoutParamsor RelativeLayout.LayoutParamsaccording to parent layout of the child view

使用LinearLayout.LayoutParamsRelativeLayout.LayoutParams根据子视图的父布局

回答by user1458071

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

can be used to set margins

可用于设置边距

回答by Klepto

The setMargin() method is available if you're using LinearLayout.LayoutParams but not if you're using ViewGroup.LayoutParams. Dipak Keshariya alludes to this but doesn't say it in so many words.

如果您使用的是 LinearLayout.LayoutParams,则 setMargin() 方法可用,但如果您使用的是 ViewGroup.LayoutParams,则不可用。Dipak Keshariya 提到了这一点,但并没有用太多的话来表达。

回答by W0rmH0le

Just sharing a slightly different approach.

只是分享一种稍微不同的方法。

Instead of casting to LinearLayout.LayoutParams, RelativeLayout.LayoutParams, etc, you can just cast to MarginLayoutParams.

您可以直接强制转换为LinearLayout.LayoutParams,而不是强制转换为、RelativeLayout.LayoutParamsMarginLayoutParams

Casting to MarginLayoutParamsis better because you can later update your layout and you don't need to return to your java code to change from LinearLayoutto RelativeLayoutor any other Layout type

强制转换MarginLayoutParams为更好,因为您可以稍后更新您的布局,并且您不需要返回到您的 java 代码来更改从LinearLayouttoRelativeLayout或任何其他布局类型

You can do something like:

您可以执行以下操作:

MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();

// Set bottom margin
layoutParams.bottomMargin = x;

// Set top margin
layoutParams.topMargin = x;

// Set left margin
// This won't have effect if you set any relative margin (start) previously or in the layout.xml
layoutParams.leftMargin = x;

// Set left margin
// This won't have effect if you set any relative margin (end) previously or in the layout.xml
layoutParams.rightMargin = x;

// Set start margin
layoutParams.setMarginStart(x);

// Set end margin
layoutParams.setMarginStart(x);

// Set all left, top, right, bottom margins at once
// Note that here, left and right margins are set (not start/end).
// So, if you have used start/end margin before (or via layout.xml), 
// setting left/right here won't have any effect.
layoutParams.setMargins(left, top, end, bottom)

// Then re-apply the layout params again to force the view to be re-draw
// This step may not be necessary because depending where you set the margin, 
// view is already scheduled to be drawn
// For any case, to ensure the view will apply the new margin, call:
view.setLayoutParams(layoutParams);

回答by Ajay Keshri

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