用代码设置 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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 学Python后到底能干什么?

    学Python后到底能干什么?Python是一种什么语言?Python是一种计算机程序设计语言。你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的Jav

    2022年7月3日
    27
  • 质量工具因果图_质量管理因果图例题

    质量工具因果图_质量管理因果图例题前言在项目中,我们经常需要用到不同的工具对项目质量进行评审。使用不同的质量工具可能得到的结果不太一样。下面简单说下项目中常用到的质量分析工具因果图。释义:什么是因果图因果图又称为石川图、Ishikawa或鱼骨图,它把影响质量诸因素之间的关系以树状图的方式表示出来,使人一目了然,便于分析原因并采取相应的措施。它是一种在问题发生后,寻找根本原因的一种方法。它取名石川图是因为它是由日

    2022年8月14日
    10
  • js替换html中的字符串,js怎么替换字符串?

    js替换html中的字符串,js怎么替换字符串?在js中,可以使用str.replace()方法来替换字符串。replace()方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串;然后返回一个新的字符串。replace()方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。语法:stringObject.replace(regexp/substr,replacement)返回值一个新的字符串…

    2022年5月18日
    43
  • 操作系统存储管理之虚拟存储与分页式虚拟存储系统

    操作系统存储管理之虚拟存储与分页式虚拟存储系统虚拟存储实现思路在实际运行过程,把有关作业的全部信息都装入主存储器后,作业执行时实际上不是同时使用全部信息的,有些部分运行一遍便再也不用,甚至有些部分在作业执行的整个过程中都不会被使用到(如错误处理部分)。进程在运行时不用的,或暂时不用的,或某种条件下才用的程序和数据,全部驻留于内存中是对宝贵的主存资源的一种浪费,大大降低了主存利用率。于是,提出了这样的问题:作业提交时,先全部进入辅助存储器,作

    2022年9月26日
    7
  • Linux下的crontab定时执行任务命令详解

    Linux下的crontab定时执行任务命令详解

    2021年10月22日
    52
  • JetBrains CLion 2021 激活码【中文破解版】

    (JetBrains CLion 2021 激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月28日
    44

发表回复

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

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