用代码设置 RelativeLayout.LayoutParams
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的更多相关文章
- 在代码设置RelativeLayout的属性,比如layout_below
( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...
- Android 代码设置RelativeLayout元素居中
RelativeLayout relativeLayout= new RelativeLayout(this); RelativeLayout.LayoutParams rlp=new Relativ ...
- 代码中动态改变布局属性RelativeLayout.LayoutParams.addRule()
我们知道,在 RelativeLayout 布局中有很多特殊的属性,通常在载入布局之前,在相关的xml文件中进行静态设置即可. 但是,在有些情况下,我们需要动态设置布局的属性,在不同的条件下设置不同的 ...
- 在代码中设置RelativeLayout布局中标签的android:layout_toLeftOf、android:layout_toRightOf等属性
需要动态改变RelativeLayout里面控件的相对位置,经一个技术群的群友提示,找到了如下的方法,做下记录: RelativeLayout.Layoutparams params = (Rel ...
- 在代码动态设置RelativeLayout的属性,比如layout_below
( (RelativeLayout.LayoutParams)holder.ivLvDivider.getLayoutParams()).addRule(RelativeLayout.BELOW, R ...
- RelativeLayout.LayoutParams.addRule()方法
1.应用场景 在使用RelativeLayout布局的时候,通常在载入布局之前在相关的XML文件中进行静态设置即可.但是,在有些情况下,我们需要动态的设置布局 的属性,在不同条件下设置不同的布局排列方 ...
- 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 ...
- Android -- java代码设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...
- android 用java代码设置布局、视图View的宽度/高度或自适应
在achat项目中,对话内容的长宽设置为自适应.可是假设文本内容太多,则宽度几乎相同布满,若自己说的和对方说的都非常多内容.则满屏都是文字.则不easy分辨出是来自别人说的还是自己说的.那么须要对本身 ...
随机推荐
- Java同步锁何时释放?
在测试java多线程中有关 “生产者和消费者” 这个经典问题的时候,写代码测试的时候,思考到一些问题(所以还是要动手,实践才能储真知啊), synchronize 同步锁何时释放,何时获得?重新获得锁 ...
- Object.getOwnPropertyNames()
1.Object.getOwnPropertyNames(),遍历实例属性(包括不可枚举),返回属性名组成的数组 var arr = ["a", "b", &q ...
- Visual Studio 2012 Fakes框架测试驱动开发TDD教程
一.前言 最近团队要尝试TDD(测试驱动开发)的实践,很多人习惯了先代码后测试的流程,对于TDD总心存恐惧,认为没有代码的情况下写测试代码时被架空了,没法写下来,其实,根据个人实践经验,TDD并不可怕 ...
- [数据集]新浪微博数据集MicroblogPCU
数据集下载地址:下载 摘要:MicroblogPCU是从新浪微博採集到的.它能够被用于研究机器学习方法和社会关系研究. 这个数据集被原作者用于探索微博中的spammers(发送垃圾信息的人).他们的d ...
- java 报错非法的前向引用
今天在看<thinking in java>的时候,第四章提到了非法的前向引用,于是自己试了一下,书中的例子倒是一下就明白了,但是自己写的一个却怎么也不明白,于是上网问了一位前辈,终于明白 ...
- Thinkpad升级Window10无法安装expresscache
本人有一台Thinkpad T440s,自从看了这篇帖子12秒开机!ExpressCache SSD缓存加速,就给自己的小黑加持了一块固态硬盘.使用后效果确实很明显. 问题 自从系统自动升级到wind ...
- Linq To Entities中的动态排序
换了工作有一个月了,一样的工作.一样的代码.一样的体力活仍就…… Linq To Entityes 也是不新玩意了,近半年来也一直与之打交道,但一直也没对其深究过.今天新加的功能要对所有列支持排序,这 ...
- docker大全集
1,什么是docker docker 最初是dotCloud公司创始人 Solomon Hykes 在法国期间发起的一个公司内部醒目,于 2013年 3 月以 Apache 2.0 授权协议开源, 主 ...
- 网络驱动移植之简述CS8900A网络芯片的基本原理
CS8900A数据手册:http://www.cirrus.com/cn/products/cs8900a.html 1.概述 CS8900A是CIRRUS LOGIC公司生产的低功耗.性能优越的16 ...
- bzoj2823: [AHOI2012]信号塔&&1336: [Balkan2002]Alien最小圆覆盖&&1337: 最小圆覆盖
首先我写了个凸包就溜了 这是最小圆覆盖问题,今晚学了一下 先随机化点,一个个加入 假设当前圆心为o,半径为r,加入的点为i 若i不在圆里面,令圆心为i,半径为0 再重新从1~i-1不停找j不在圆里面, ...