How to set layout_margin programmatically? [duplicate]

白昼怎懂夜的黑 提交于 2019-12-04 15:35:24

问题


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

Thanks Monali


回答1:


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


来源:https://stackoverflow.com/questions/5788551/how-to-set-layout-margin-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!