博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RelativeLayout addRule踩坑之旅
阅读量:5789 次
发布时间:2019-06-18

本文共 5544 字,大约阅读时间需要 18 分钟。

Android设置一些属性时,可以使用|来表示同时设置的功能。 比如Gravity

private int mGravity = Gravity.START | Gravity.TOP;复制代码

最近项目中代码改动,将xml的view,改成在java代码中添加。 写成了下面代码

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(ALIGN_PARENT_BOTTOM | ALIGN_PARENT_RIGHT);复制代码

导致布局出错。 查看源代码

/**         * Adds a layout rule to be interpreted by the RelativeLayout.         * 

* Use this for verbs that refer to a sibling (ex. * {@link #ALIGN_RIGHT}) or take a boolean value (ex. * {@link #CENTER_IN_PARENT}). *

* If the rule is relative to the layout direction (ex. * {@link #START_OF}), then the layout direction must be resolved using * {@link #resolveLayoutDirection(int)} before calling * {@link #getRule(int)} with an absolute rule (ex. {@link #LEFT_OF}. * * @param verb a layout verb, such as {@link #ALIGN_RIGHT} * @param subject the ID of another view to use as an anchor, or a * boolean value (represented as {@link #TRUE} for true * or 0 for false) * @see #addRule(int) * @see #removeRule(int) * @see #getRule(int) */ public void addRule(int verb, int subject) { // If we're removing a relative rule, we'll need to force layout // resolution the next time it's requested. if (!mNeedsLayoutResolution && isRelativeRule(verb) && mInitialRules[verb] != 0 && subject == 0) { mNeedsLayoutResolution = true; } mRules[verb] = subject; mInitialRules[verb] = subject; mRulesChanged = true; }复制代码

Android使用的是数组,对Rule进行的缓存,即规则是一一对应的。

/**     * Rule that aligns a child's right edge with another child's left edge.     */    public static final int LEFT_OF                  = 0;    /**     * Rule that aligns a child's left edge with another child's right edge.     */    public static final int RIGHT_OF                 = 1;    /**     * Rule that aligns a child's bottom edge with another child's top edge.     */    public static final int ABOVE                    = 2;    /**     * Rule that aligns a child's top edge with another child's bottom edge.     */    public static final int BELOW                    = 3;    /**     * Rule that aligns a child's baseline with another child's baseline.     */    public static final int ALIGN_BASELINE           = 4;    /**     * Rule that aligns a child's left edge with another child's left edge.     */    public static final int ALIGN_LEFT               = 5;    /**     * Rule that aligns a child's top edge with another child's top edge.     */    public static final int ALIGN_TOP                = 6;    /**     * Rule that aligns a child's right edge with another child's right edge.     */    public static final int ALIGN_RIGHT              = 7;    /**     * Rule that aligns a child's bottom edge with another child's bottom edge.     */    public static final int ALIGN_BOTTOM             = 8;    /**     * Rule that aligns the child's left edge with its RelativeLayout     * parent's left edge.     */    public static final int ALIGN_PARENT_LEFT        = 9;    /**     * Rule that aligns the child's top edge with its RelativeLayout     * parent's top edge.     */    public static final int ALIGN_PARENT_TOP         = 10;    /**     * Rule that aligns the child's right edge with its RelativeLayout     * parent's right edge.     */    public static final int ALIGN_PARENT_RIGHT       = 11;    /**     * Rule that aligns the child's bottom edge with its RelativeLayout     * parent's bottom edge.     */    public static final int ALIGN_PARENT_BOTTOM      = 12;    /**     * Rule that centers the child with respect to the bounds of its     * RelativeLayout parent.     */    public static final int CENTER_IN_PARENT         = 13;    /**     * Rule that centers the child horizontally with respect to the     * bounds of its RelativeLayout parent.     */    public static final int CENTER_HORIZONTAL        = 14;    /**     * Rule that centers the child vertically with respect to the     * bounds of its RelativeLayout parent.     */    public static final int CENTER_VERTICAL          = 15;    /**     * Rule that aligns a child's end edge with another child's start edge.     */    public static final int START_OF                 = 16;    /**     * Rule that aligns a child's start edge with another child's end edge.     */    public static final int END_OF                   = 17;    /**     * Rule that aligns a child's start edge with another child's start edge.     */    public static final int ALIGN_START              = 18;    /**     * Rule that aligns a child's end edge with another child's end edge.     */    public static final int ALIGN_END                = 19;    /**     * Rule that aligns the child's start edge with its RelativeLayout     * parent's start edge.     */    public static final int ALIGN_PARENT_START       = 20;    /**     * Rule that aligns the child's end edge with its RelativeLayout     * parent's end edge.     */    public static final int ALIGN_PARENT_END         = 21;复制代码

每个规则对应数组的下标,所以不能进行位运算。

记录一下,防止以后再惯性思维导致出错。

需要分别调用addRule添加

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT,                ViewGroup.LayoutParams.WRAP_CONTENT);lp.addRule(ALIGN_PARENT_BOTTOM);lp.addRule(ALIGN_PARENT_RIGHT);复制代码

转载地址:http://gwhyx.baihongyu.com/

你可能感兴趣的文章
centos7根分区扩容(适用目前主流版本)
查看>>
linux--pxe 安装系统
查看>>
ASPF(Application Specific Packet Filter)
查看>>
Jquery中缩小图像的大小
查看>>
logrotate日志自动分割
查看>>
linux命令——cp
查看>>
视频直播常见问题与解决办法汇总【系列一】
查看>>
【问题】The coprocessor thread stopped itself due to scan timeout or scan threshold
查看>>
jQuery | 获取及设置内容和属性
查看>>
wget的替换工具axel多线程下载详解
查看>>
day04.2:linux克隆及服务器之间登录
查看>>
回归测试自动化不充分-Bug总结系列笔记
查看>>
zabbix 监控mysql状态
查看>>
HAWQ技术总结
查看>>
如何在Office 365租户之间迁移邮箱
查看>>
loadRunner_windows Sockers 关联
查看>>
Android开发之旅: Intents和Intent Filters(实例部分)
查看>>
WinEyes的重新实现--windows系统及其消息机制
查看>>
Mysql的共享锁和排他锁(转载)
查看>>
对于B-Tree B+Tree 红黑二叉树我的理解
查看>>