如何设置layout_margin编程? [英] How to set layout_margin programmatically?

查看:89
本文介绍了如何设置layout_margin编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何layout_marginLeft,layout_marginTop,layout_marginBottom编程方式使用屏幕的高度和宽度来设置。请帮我。

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

谢谢 Monali

推荐答案

下面是一个例子(摘自<一个href="http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically/3002321#3002321">this答案):

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

这篇关于如何设置layout_margin编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆