如何在Relative Layout容器中添加另一个视图?

时间:2013-10-17 17:55:45

标签: android

我有一个带有子视图的RelativeLayout容器,如下所示:

[ -> begin of relative layout containner
 [view] -> view (LinearLayout)
 [view] -> view (LinearLayout) + rule (below of previous view)
 [view] -> view (LinearLayout) + rule (below of previous view)
] -> end of relative layout

所有内容都是以编程方式生成的。

事件发生后,我需要在两个子视图之间添加一个子视图。我做到了:

public void add(PerguntaViewContainner parent, PerguntaViewContainner child) {
    PerguntaViewContainner previousChild = parent;
    PerguntaViewContainner nextChild =  HocusPocus.getNextElementOf(parent);

    perguntaParams.addRule(RelativeLayout.BELOW, previousChild.getId());
    perguntaParams.addRule(RelativeLayout.ABOVE, nextChild.getId());

    containner.addView(child, perguntaParams);
}

但孩子没有露面。你知道吗,为什么会发生?

1 个答案:

答案 0 :(得分:0)

看起来您可能还需要更正其他子参数,如下所示:

public void add(PerguntaViewContainner parent, PerguntaViewContainner child) {
    PerguntaViewContainner previousChild = parent;
    PerguntaViewContainner nextChild =  HocusPocus.getNextElementOf(parent);

    perguntaParams.addRule(RelativeLayout.BELOW, previousChild.getId());
    perguntaParams.addRule(RelativeLayout.ABOVE, nextChild.getId());

    containner.addView(child, perguntaParams);

    final RelativeLayout.LayoutParams nextViewParams = (RelativeLayout.LayoutParams)nextChild.getLayoutParams();

    nextViewParams.addRule(RelativeLayout.BELOW, child.getId())
    nextChild.setLayoutParams(nextViewParams);
}

但是,问题并不完整,您的自定义布局很容易出现问题。