2013-10-17 56 views
0

我有孩子意見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); 
} 

但是孩子沒有出現。你有什麼想法,爲什麼會發生?

+0

什麼是PerguntaViewContainner和PerguntaViewContainner?它們如何與RelativeLayout和LinearLayout相關,以及它們爲什麼不在具有視圖結構的'圖表'中提供? – sandrstar

回答

0

看起來你可能需要更正其他孩子也PARAMS,如下所示:

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); 
} 

然而,問題是不完整的,它可以輕鬆地與您的自定義佈局的任何問題。

相關問題