我们平常可以直接在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. 条件随机场(Conditional random field)

    条件随机场真是把我给折磨坏了啊,本以为一本小小的<统计学习方法>攻坚剩下最后一章,心情还是十分愉悦的,打算一口气把它看完,结果真正啃起来真是无比的艰难啊,每一句对我都好像是天书一般,怎么这 ...

  2. 服务器tomcat配置教程

    2018年上学期期末课程设计做了一个留言板,但是我需要把这个Jave Web弄到我的服务器上 首先我们可以安装jdk tomcat在启动时,会读取环境变量的信息,需要一个CATALINA_HOME 与 ...

  3. Scala 基础(1)—— 定义变量 & 定义函数

    1. 使用 val & var 定义变量 Scala 中的变量被分为2种:val 和 var.其含义于 Java 中的 final 关键字类似. val 等同于被 final 修饰过的变量, ...

  4. 【bzoj3829】[Poi2014]FarmCraft 贪心

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are   ...

  5. P3141 [USACO16FEB]围栏Fenced In_Platinum

    题目描述 Farmer John has realized that many of his cows are strangely agoraphobic (being fearful of larg ...

  6. 2017 多校6 String

    多校6 String(ac自动机) 题意: 给一本有\(n\)个单词的字典 \(q\)个查询 \(pref_i,suff_i\) 查询字典里有多少单词前缀匹配\(pref_i\),后缀同时匹配\(su ...

  7. gerrit工具-workflow

    gerrit-workflow

  8. 学习Git的一点心得以及如何把本地修改、删除的代码上传到github中

    一:学习Github的资料如下:https://git.oschina.net/progit/ 这是一个学习Git的中文网站,如果诸位能够静下心来阅读,不要求阅读太多,只需要阅读前三章,就可以掌握Gi ...

  9. Linux临时增加swap空间

    linux临时增加swap空间:step 1: #dd if=/dev/zero of=/home/swap bs=1024 count=500000 注释:of=/home/swap,放置swap的 ...

  10. 转 android Listview,gridview局部刷新,部分刷新 (仿QQ动态评论局部刷新)

    转自:http://blog.csdn.net/jdsjlzx/article/details/45582719 众所周知Listview和Gridview的刷新界面的方式是调用adapter.not ...