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.
* <p>
* Use this for verbs that refer to a sibling (ex.
* {@link #ALIGN_RIGHT}) or take a boolean value (ex.
* {@link #CENTER_IN_PARENT}).
* <p>
* 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);
复制代码