我们平常可以直接在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. Ironic-Python-Agent

    Ironic-Python-Agent 在PXE部署环境中,deploy模块是通过打开一个iSCSI设备,ironic-conductro将OS的镜像文件写到iSCSI的设备,所以deploy_ram ...

  2. CI框架入门

    本人最近在学习CI框架,网上找到一些个人觉得入门比较好的资料,记录一下: 兄弟连的CI框架入门系类: [军哥谈CI框架]之入门教程之第一讲:codeigniter的介绍和安装配置:http://bbs ...

  3. django ORM中的表关系

    多对一: 为了方便理解,两个表之间使用ForeignKey连接时,使用ForeignKey的字段所在的表为从表,被ForeignKey连接的表为主表. 使用场景:书和出版社之间的关系,一本书只能由一个 ...

  4. ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 E: IQ(iq)

    http://acm.ocrosoft.com/problem.php?cid=1316&pid=4 题目描述 根据世界某权威学会的一项调查,学信息学的学生IQ非常高.举个最好的例子,如果我们 ...

  5. openssl unicode编译以及VC++2015环境下的问题

    这几天需要使用openssl,前期本机上保存的目录不知道哪天整理的时候删除了,索性下载最新的自己编译一下: 在最新版的openssl(openssl-1.0.2e),编译过程中出现了很多问题,这里主要 ...

  6. SQL Server 重新编译存储过程的方式有三种

    SQL Server 中,强制重新编译存储过程的方式有三种: sp_recompile 系统存储过程强制在下次执行存储过程时对其重新编译.具体方法是:从过程缓存中删除现有计划,强制在下次运行该过程时创 ...

  7. [洛谷P3807]【模板】卢卡斯定理

    题目大意:给你$n,m,p(p \in \rm prime)$,求出$C_{n + m}^m\bmod p(可能p\leqslant n,m)$ 题解:卢卡斯$Lucas$定理,$C_B^A\bmod ...

  8. Codeforces Round #324 (Div. 2) A

    A. Olesya and Rodion time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 3.1 Java以及Lucene的安装与配置

    Lucene是Java开发的一套用于全文检索和搜索的开源程序库,它面向对象多层封装,提供了一个低耦合.与平台无关的.可进行二次开发的全文检索引擎架构,是这几年最受欢迎的信息检索程序库[1].对Luce ...

  10. Vitamio介绍及使用

    一.Vitamio介绍 1.1 Vitamio是什么? Vitamio是Android平台视音频播放组件,支持播放几乎格式的视频以及主流网络视频流(http/rtsp/mms等),详细的中文介绍: 这 ...