1.注意

  • 不能在RelativeLayout容器本身和他的子元素之间产生循环依赖,比如说,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高设置成为 ALIGN_PARENT_BOTTOM。
  • 如果parent是wrap_content的话,alignParentRight就意味着要求parent的layout_width="match_parent"; alignParentBottom就意味着要求parent的layout_height="match_parent"

  等等。

2.用代码设置 RelativeLayout.LayoutParams

         RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) holder.layout.getLayoutParams();

         //如果之前设置了rule,同时设置了多个rules可能冲突,下面代码清空rules
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){// >= 17
params3.removeRule(RelativeLayout.RIGHT_OF);
}else{
int rules[] = params3.getRules();
rules[RelativeLayout.LEFT_OF] = ;
rules[RelativeLayout.RIGHT_OF] = ;
}
//添加新的rules,一般刚创建RelativeLayout.LayoutParams时,不要添加任何rule
if (mb.contact.isSelf()) {
params3.addRule(RelativeLayout.LEFT_OF,R.id.msg_portrait);
}else{
params3.addRule(RelativeLayout.RIGHT_OF,R.id.msg_portrait);
}
holder.layout.setLayoutParams(params3);

3.常用的rule

     /**
* Rule that aligns a child's right edge with another child's left edge.
*/
public static final int LEFT_OF = ;
/**
* Rule that aligns a child's left edge with another child's right edge.
*/
public static final int RIGHT_OF = ;
/**
* Rule that aligns a child's bottom edge with another child's top edge.
*/
public static final int ABOVE = ;
/**
* Rule that aligns a child's top edge with another child's bottom edge.
*/
public static final int BELOW = ; /**
* Rule that aligns a child's baseline with another child's baseline.
*/
public static final int ALIGN_BASELINE = ;
/**
* Rule that aligns a child's left edge with another child's left edge.
*/
public static final int ALIGN_LEFT = ;
/**
* Rule that aligns a child's top edge with another child's top edge.
*/
public static final int ALIGN_TOP = ;
/**
* Rule that aligns a child's right edge with another child's right edge.
*/
public static final int ALIGN_RIGHT = ;
/**
* Rule that aligns a child's bottom edge with another child's bottom edge.
*/
public static final int ALIGN_BOTTOM = ; /**
* Rule that aligns the child's left edge with its RelativeLayout
* parent's left edge.
*/
public static final int ALIGN_PARENT_LEFT = ;
/**
* Rule that aligns the child's top edge with its RelativeLayout
* parent's top edge.
*/
public static final int ALIGN_PARENT_TOP = ;
/**
* Rule that aligns the child's right edge with its RelativeLayout
* parent's right edge.
*/
public static final int ALIGN_PARENT_RIGHT = ;
/**
* Rule that aligns the child's bottom edge with its RelativeLayout
* parent's bottom edge.
*/
public static final int ALIGN_PARENT_BOTTOM = ; /**
* Rule that centers the child with respect to the bounds of its
* RelativeLayout parent.
*/
public static final int CENTER_IN_PARENT = ;
/**
* Rule that centers the child horizontally with respect to the
* bounds of its RelativeLayout parent.
*/
public static final int CENTER_HORIZONTAL = ;
/**
* Rule that centers the child vertically with respect to the
* bounds of its RelativeLayout parent.
*/
public static final int CENTER_VERTICAL = ;
/**
* Rule that aligns a child's end edge with another child's start edge.
*/
public static final int START_OF = ;
/**
* Rule that aligns a child's start edge with another child's end edge.
*/
public static final int END_OF = ;
/**
* Rule that aligns a child's start edge with another child's start edge.
*/
public static final int ALIGN_START = ;
/**
* Rule that aligns a child's end edge with another child's end edge.
*/
public static final int ALIGN_END = ;
/**
* Rule that aligns the child's start edge with its RelativeLayout
* parent's start edge.
*/
public static final int ALIGN_PARENT_START = ;
/**
* Rule that aligns the child's end edge with its RelativeLayout
* parent's end edge.
*/
public static final int ALIGN_PARENT_END = ;

用代码设置 RelativeLayout.LayoutParams的更多相关文章

  1. 在代码设置RelativeLayout的属性,比如layout_below

    ( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...

  2. Android 代码设置RelativeLayout元素居中

    RelativeLayout relativeLayout= new RelativeLayout(this); RelativeLayout.LayoutParams rlp=new Relativ ...

  3. 代码中动态改变布局属性RelativeLayout.LayoutParams.addRule()

    我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...

  4. 在代码中设置RelativeLayout布局中标签的android:layout_toLeftOf、android:layout_toRightOf等属性

    需要动态改变RelativeLayout里面控件的相对位置,经一个技术群的群友提示,找到了如下的方法,做下记录:   RelativeLayout.Layoutparams params = (Rel ...

  5. 在代码动态设置RelativeLayout的属性,比如layout_below

    ( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...

  6. RelativeLayout.LayoutParams.addRule()方法

    1.应用场景 在使用RelativeLayout布局的时候,通常在载入布局之前在相关的XML文件中进行静态设置即可.但是,在有些情况下,我们需要动态的设置布局 的属性,在不同条件下设置不同的布局排列方 ...

  7. java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

    java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.w ...

  8. Android -- java代码设置margin

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

  9. android 用java代码设置布局、视图View的宽度/高度或自适应

    在achat项目中,对话内容的长宽设置为自适应.可是假设文本内容太多,则宽度几乎相同布满,若自己说的和对方说的都非常多内容.则满屏都是文字.则不easy分辨出是来自别人说的还是自己说的.那么须要对本身 ...

随机推荐

  1. 对dispatch_async到主线程的逻辑封装成C/C++接口类型

    背景:代码里面有时候会把将要运行的内容放到主线程里面运行,但假设已经是主线程里面的代码调用dispatch_async的时候偶尔会出现crash,所以就须要推断是否已经在主线程里面了. 通常的做法类似 ...

  2. python05-09

    一.lambda表达式 def f1(): return 123 f2 = lambda : 123 def f3 = (a1,a2): return a1+a2 f4 = lambda a1,a2 ...

  3. Struts2框架起源

    曾经也用过S2SH框架做过几个项目,都不是工作中的,学习WEB开发的时候接触的第一套框架也是S2SH,可是工作之后一直没实用到S2SH 框架进行开发. 感觉曾经用这个框架的时候根本没有深入去了解这个框 ...

  4. 为什么java构造函数的构造器只能在第一行写this() 或者super() ?

    最近在看内部类, 但是被“为什么匿名内部类或者局部内部类使用方法的局部变量时, 局部变量一定得是final类型”困扰着, 在网上查找资料的时候, 发现我对类初始化完全不了解, 之前的认识都是错误! 所 ...

  5. Part1-Redefining your data-access strategy 重新定义你的数据访问策略

    欢迎来到Entity Framework 4 In Action,EF是微软3.5 SP1推出的ORM工具,现在已经更新到4.0版本(...)本书能确保你in a  robust and model- ...

  6. 20170111 ABAP技术小结(全半角转换)

    DATA: it_po LIKE it_alv OCCURS 0 WITH HEADER LINE.************************************************** ...

  7. XMU 1606 nc与滴水问题 【模拟】

    1606: nc与滴水问题 Time Limit: 1000 MS  Memory Limit: 64 MBSubmit: 85  Solved: 27[Submit][Status][Web Boa ...

  8. HttpsURLConnection 安全传输(HTTPS--Secure Hypertext Transfer Protocol-安全超文本传输协议)

    HttpsURLConnection 扩展 HttpURLConnection,支持各种特定于 https 功能.此类使用 HostnameVerifier 和 SSLSocketFactory.为这 ...

  9. ssh配置无密码登录

    1.在master机器上生成公钥: [root@master ~]# ssh-keygen -t rsa    注:一直按enter键就可以生成了 Generating public/private ...

  10. 思维导图js

    http://baike.baidu.com/view/1469162.htm?fromenter=Mind+Maps