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. Java知识图谱

    1.Java学习路径1 我想很多人看到这个路径可能会问我在哪里可以学习,所以就先附上这条路径的学习地址吧,这也是这张图片的来源,愿意学习的可以去看看:Java研发工程师学习路径 2.Java学习路径2 ...

  2. CentOS 5 全功能服务器搭建

    转自: http://www.php-oa.com/2007/12/27/centos-www.html 转:主要做为历史记录,以后用.另外很少见这么好的编译的文章,其实我不推荐用编译安装.但这个文章 ...

  3. window.location.hashs属性介绍

    长话短说. location是javascript里边管理地址栏的内置对象.比方location.href就管理页面的url,用location.href=url就能够直接将页面重定向url. 而lo ...

  4. php验证邮箱

    <?php if(isset($_POST['email'])){ $email = $_POST['email']; if(filter_var($email, FILTER_VALIDATE ...

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

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

  6. the first week study

    1.In 1989, a man named Guido create "python" as a kind of computer languages. And now we u ...

  7. Android 官方博客 - Android应用程序的内存分析(翻译)(转)

    转自:http://www.cnblogs.com/wisekingokok/archive/2011/11/30/2245790.html Dalvik虚拟机支持垃圾收集,但是这不意味着你可以不用关 ...

  8. while语句字符串的基本操作

    1,编码:对现在通用文字编码成计算机文字,便于储存,传递,交流. 最早的计算机编码是ACSII美国人创建的,包含英文字母,数字,以及特殊符号.总共是128个码位:2**7,因为计算机的底层只能识别:& ...

  9. (5)在tomcat运行自己的javaweb项目

    A:在MyEclipse下方的Servers栏中启动服务器,运行项目: 1,选中项目所在的tomcat服务器 2,点击“启动按钮”,见下图 3,启动以后,看控制台输出日志: B:从服务器按钮启动: 1 ...

  10. 关于Vim的一个配置文件

    昨天晚上+今天早上怒赶了一份关于Vim的自动化配置的Shell脚本,之前在github上见过一个这么一个类似的脚本项目,然后又见到同校的有一位师兄也写过这么一个类似的脚本文件,然后我也抽分跟着写一份属 ...