用代码设置 RelativeLayout.LayoutParams

用代码设置 RelativeLayout.LayoutParams1.注意不能在RelativeLayout容器本身和他的子元素之间产生循环依赖,比如说,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高设置成为ALIGN_PARENT_BOTTOM。如果parent是wrap_content的话,alignParentRight就意味着要求parent的layout_width=”match_par…

大家好,又见面了,我是你们的朋友全栈君。

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

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

3.常用的rule

  1     /**
  2      * Rule that aligns a child's right edge with another child's left edge.
  3      */
  4     public static final int LEFT_OF                  = 0;
  5     /**
  6      * Rule that aligns a child's left edge with another child's right edge.
  7      */
  8     public static final int RIGHT_OF                 = 1;
  9     /**
 10      * Rule that aligns a child's bottom edge with another child's top edge.
 11      */
 12     public static final int ABOVE                    = 2;
 13     /**
 14      * Rule that aligns a child's top edge with another child's bottom edge.
 15      */
 16     public static final int BELOW                    = 3;
 17 
 18     /**
 19      * Rule that aligns a child's baseline with another child's baseline.
 20      */
 21     public static final int ALIGN_BASELINE           = 4;
 22     /**
 23      * Rule that aligns a child's left edge with another child's left edge.
 24      */
 25     public static final int ALIGN_LEFT               = 5;
 26     /**
 27      * Rule that aligns a child's top edge with another child's top edge.
 28      */
 29     public static final int ALIGN_TOP                = 6;
 30     /**
 31      * Rule that aligns a child's right edge with another child's right edge.
 32      */
 33     public static final int ALIGN_RIGHT              = 7;
 34     /**
 35      * Rule that aligns a child's bottom edge with another child's bottom edge.
 36      */
 37     public static final int ALIGN_BOTTOM             = 8;
 38 
 39     /**
 40      * Rule that aligns the child's left edge with its RelativeLayout
 41      * parent's left edge.
 42      */
 43     public static final int ALIGN_PARENT_LEFT        = 9;
 44     /**
 45      * Rule that aligns the child's top edge with its RelativeLayout
 46      * parent's top edge.
 47      */
 48     public static final int ALIGN_PARENT_TOP         = 10;
 49     /**
 50      * Rule that aligns the child's right edge with its RelativeLayout
 51      * parent's right edge.
 52      */
 53     public static final int ALIGN_PARENT_RIGHT       = 11;
 54     /**
 55      * Rule that aligns the child's bottom edge with its RelativeLayout
 56      * parent's bottom edge.
 57      */
 58     public static final int ALIGN_PARENT_BOTTOM      = 12;
 59 
 60     /**
 61      * Rule that centers the child with respect to the bounds of its
 62      * RelativeLayout parent.
 63      */
 64     public static final int CENTER_IN_PARENT         = 13;
 65     /**
 66      * Rule that centers the child horizontally with respect to the
 67      * bounds of its RelativeLayout parent.
 68      */
 69     public static final int CENTER_HORIZONTAL        = 14;
 70     /**
 71      * Rule that centers the child vertically with respect to the
 72      * bounds of its RelativeLayout parent.
 73      */
 74     public static final int CENTER_VERTICAL          = 15;
 75     /**
 76      * Rule that aligns a child's end edge with another child's start edge.
 77      */
 78     public static final int START_OF                 = 16;
 79     /**
 80      * Rule that aligns a child's start edge with another child's end edge.
 81      */
 82     public static final int END_OF                   = 17;
 83     /**
 84      * Rule that aligns a child's start edge with another child's start edge.
 85      */
 86     public static final int ALIGN_START              = 18;
 87     /**
 88      * Rule that aligns a child's end edge with another child's end edge.
 89      */
 90     public static final int ALIGN_END                = 19;
 91     /**
 92      * Rule that aligns the child's start edge with its RelativeLayout
 93      * parent's start edge.
 94      */
 95     public static final int ALIGN_PARENT_START       = 20;
 96     /**
 97      * Rule that aligns the child's end edge with its RelativeLayout
 98      * parent's end edge.
 99      */
100     public static final int ALIGN_PARENT_END         = 21;

 

转载于:https://www.cnblogs.com/sjjg/p/5807746.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160844.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何黑钓鱼网站_初学钓鱼视频教程全集

    如何黑钓鱼网站_初学钓鱼视频教程全集一、被盗号找上门今天一打开QQ邮箱,看到个群邮件这货以发聚会照片为名义给出了一个网址Defaulthttp://202.194.131.20/xcb/xcb/link.php?id=73&url=http://yyhhj.emy.in/cc/?9330461http://202.194.131.20/xcb/xcb/link.php?id=73&url=http://yyhhj.e…

    2022年8月24日
    4
  • 系统调用(int 0x80)详解

    系统调用(int 0x80)详解1、系统调用初始化在系统启动时,会在sched_init(void)函数中调用set_system_gate(0x80,&system_call),设置中断向量号0x80的中断描述符:#defineset_system_gate(n,addr) _set_gate(&idt[n],15,3,addr)其中15表示此中断号对应的是陷阱门,注意,这个中断向量不是中断门描述符

    2022年9月13日
    0
  • IDEA更换主题

    IDEA更换主题更换IDEA主题只需要3步(IDEA14)1.下载主题在主题网站上IDEAColorThemes上浏览喜欢的主题并下载该主题。(如果网址有变更,googleIDEAthemes即可。)2.导入主题在IDEA中导入该主题,具体如下:file–>importsetttings–>选中1中下载的主题jar文件–>一路确认–>重启3.设置主题设置主题:

    2022年5月31日
    110
  • ubuntu16.04配置本地镜像源_修改ubuntu镜像源

    ubuntu16.04配置本地镜像源_修改ubuntu镜像源一、Ubuntu版本和代号:Ubuntu可谓是Linux世界中的黑马,其第一个正式版本于2004年10月正式推出。需要详细解释的是Ubuntu版本编号的定义,其编号以“年份的最后一位.发布月份”的格式命名,因此Ubuntu的第一个版本就称为4.10(2004.10)。除了代号之外,每个Ubuntu版本在开发之初还有一个开发代号。Ubuntu开发代号比较有意思,格式为“形容词+动物”,且形容词和动物名称的第一个字母要一致,如Ubuntu16.04的开发代号是XenialXerus,译为“好客的非洲地松鼠

    2022年10月14日
    0
  • 从listView1中选择记录到listView2中「建议收藏」

    从listView1中选择记录到listView2中「建议收藏」publicclassSelectWorker{publicintpAreaID,WorkerID;publicstringpArea,WkName;}

    2022年7月26日
    7
  • 歌词编辑软件哪个好_编歌词软件

    歌词编辑软件哪个好_编歌词软件https://www.cnblogs.com/BensonLaur/p/6262565.html 感谢开发这么好的工具!下载地址:http://beslyric.320.io/BesBlog/beslyric/download.action 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言、评论转载于:https://www.cnblogs….

    2025年7月13日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号