如何以编程方式解决RelativeLayout中不存在的循环依赖关系?

时间:2019-02-25 15:03:25

标签: android android-layout android-relativelayout

我正在实现一个Android应用,该应用无需使用横向模式布局即可支持横向和纵向模式。就像在人像模式下进行横向美化的假视图一样。对于逆横向模式,我需要将视图的位置从右侧更改为左侧,并将ui的左侧视图更改为右侧。我尝试删除LayoutParam规则,如果该视图引用到父RelativeLayout(例如RelativeLayout.ALIGN_PARENT_LEFT),则效果很好,但是如果一个视图引用了第二个视图, 像RelativeLayout.RIGHT_OF,LEFT_OF一样会给出错误,

IllegalStateException: Circular dependencies cannot exist in RelativeLayout

如何以编程方式删除循环引用?

private void removeLayoutParamsRules(View view, LayoutRule... rules){
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
    for (int i=0; i<rules.length; i++){
        lp.removeRule(rules[i].getVerb());
    }
    view.setLayoutParams(lp);
}

private void addRules(View view, LayoutRule... rules)throws IllegalStateException{
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
    if (rules!=null){
        for (LayoutRule rule:rules){
            if (rule.getSubject()!=-1){
                lp.addRule(rule.getVerb(), rule.getSubject());
            }else {
                lp.addRule(rule.getVerb());
            }
        }
    }
    view.setLayoutParams(lp);
}

public class LayoutRule {
@Getter @Setter
int verb;
@Getter @Setter
int subject = -1;
public LayoutRule(int verb, int subject){
    this.verb = verb;
    this.subject = subject;
}

}

0 个答案:

没有答案