我们平常可以直接在xml里设置margin,如:

<ImageView android:layout_margin="5dip" android:src="@drawable/image" />

但是有些情况下,需要在java代码里来写,可是View本身没有setMargin方法,怎么办呢?

通过查阅android api,我们发现android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom).

其直接的子类有: FrameLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams.

使用方法:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 20, 30, 40);
imageView.setLayoutParams(lp);

原文:http://greatverve.cnblogs.com/archive/2012/01/29/android-margin.html

android 使用代码实现 RelativeLayout布局

 RelativeLayout rl = new RelativeLayout(this);

        Button btn1 = new Button(this);
btn1.setText("----------------------");
btn1.setId(1); RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
// btn1 位于父 View 的顶部,在父 View 中水平居中
rl.addView(btn1, lp1 ); Button btn2 = new Button(this);
btn2.setText("|\n|\n|\n|\n|\n|");
btn2.setId(2); RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, 1);
lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
// btn2 位于 btn1 的下方、其左边和 btn1 的左边对齐
rl.addView(btn2, lp2); Button btn3 = new Button(this);
btn3.setText("|\n|\n|\n|\n|\n|");
btn3.setId(3); RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.BELOW, 1);
lp3.addRule(RelativeLayout.RIGHT_OF, 2);
lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
// btn3 位于 btn1 的下方、btn2 的右方且其右边和 btn1 的右边对齐(要扩充)
rl.addView(btn3,lp3); Button btn4 = new Button(this);
btn4.setText("--------------------------------------------");
btn4.setId(4); RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp4.addRule(RelativeLayout.BELOW, 2);
lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
// btn4 位于 btn2 的下方,在父 Veiw 中水平居中
rl.addView(btn4,lp4); setContentView(rl);

原文:http://kukuqiu.iteye.com/blog/1018396

动态修改RelativeLayout的宽高:

参数可以.属性设置,但数值是像素,需要转化为dp单位。

RelativeLayout.LayoutParams linearParams =  (RelativeLayout.LayoutParams)rela_addnote_notetype.getLayoutParams();
linearParams.height = 44;
rela_addnote_notetype.setLayoutParams(linearParams);

原文:http://blog.csdn.net/chenguang79/article/details/37874793

Android适配所需知识点LayoutParams:

http://www.android100.org/html/201406/05/18971.html

在java代码中设置margin的更多相关文章

  1. 转--Android如何在java代码中设置margin

    ========  3 在Java代码里设置button的margin(外边距)? 1.获取按钮的LayoutParams LinearLayout.LayoutParams layoutParams ...

  2. Android如何在java代码中设置margin

    习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:s ...

  3. Android在java代码中设置margin

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  4. android中在java代码中设置Button按钮的背景颜色

    android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...

  5. 详解Spring中的CharacterEncodingFilter--forceEncoding为true在java代码中设置失效--html设置编码无效

    在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodin ...

  6. android中在代码中设置margin属性

    1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...

  7. android如何在代码中设置margin

    1.首先看你要设置组件的父容器是什么. 例如,我是FrameLayout其中包括组件.如果调用bottomLayout这是: FrameLayout.LayoutParams lp = new Fra ...

  8. java 代码中设置 临时 环境变量

    System.setProperty("hadoop.home.dir", "D:\\software\\software_install\\dev_install\\h ...

  9. Java代码中获取Json的key值

    测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...

随机推荐

  1. PAT——乙级1028

    这道题花了我半个多小时,对呀乙级算是挺多时间的了. 1028 人口普查 (20 point(s)) 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个 ...

  2. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

  3. PAT——甲级1046S:shortest Distance

    这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N ex ...

  4. URAL 1944 大水题模拟

    D - Record of the Attack at the Orbit Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format ...

  5. 细说php2[正则表达式学习笔记]

    <细说php>这本书应该是每个php程序员入门的必读书籍,里面讲的很多知识都很系统和详细,看了正则这部分,并练习了里面的案例,发现自己已经会了很多.... header('Content- ...

  6. 应用交付工程师Troubleshooting经验分享

    应用交付工程师Troubleshooting经验分享 来源:http://blog.51cto.com/virtualadc/1188328 来源:http://blog.51cto.com/virt ...

  7. bzoj 4897 天赋 有向图的矩阵数定理

    4894: 天赋 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 104  Solved: 80[Submit][Status][Discuss] De ...

  8. PotPlayer一款简洁好用的播放器

    PotPlayer 是 KMPlayer 的原制作者姜龙喜先生(韩国)进入 Daum 公司后的新一代作品.PotPlayer 的优势在于强大的内置解码器:而 KMPlayer 的优势在于强大的定制能力 ...

  9. hdu 5072 计数+容斥原理

    /* 题意: 给出n个数(n<100000), 每个数都不大于100000,数字不会有重复.现在随意抽出3个,问三个彼此互质 或者 三个彼此不互质的数目有多少. 思路: 这道题反着想,就是三个数 ...

  10. 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest(solved 8/11)

    这套题似乎是省选前做的,一直没来写题解---补上补上>_< 链接:http://codeforces.com/gym/101147 一样先放上惨不忍睹的成绩好了--- Problem A ...