开发者

How to set layout_margin programmatically? [duplicate]

开发者 https://www.devze.com 2023-02-28 22:53 出处:网络
This question already has answers here: Set margins in a LinearLayout programmatically (11 answers) 开发者_如何学C
This question already has answers here: Set margins in a LinearLayout programmatically (11 answers) 开发者_如何学C Closed 8 years ago.

I want to know how to set layout_marginLeft, layout_marginTop, layout_marginBottom programmatically using screen height and width. Please help me.

Thanks Monali


Here is an example (adapted from this answer):

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

params.setMargins(10, 20, 30, 40);

Button button = new Button(this);
button.setText("some text");
layout.addView(button, params);
0

精彩评论

暂无评论...
验证码 换一张
取 消