addView在Android中不工作

addView在Android中不工作,android,Android,以上是自定义视图组中的一个方法。添加的播放器组件不显示。我不明白那有什么不对。请帮忙 public void addPlayer(String playerName,String balance,String gender){ try{ LayoutParams playerParams = new LayoutParams(PlayerComponent.LayoutParams.WRAP_CONTENT, PlayerView.

以上是自定义视图组中的一个方法。添加的播放器组件不显示。我不明白那有什么不对。请帮忙

    public void addPlayer(String playerName,String balance,String gender){
            try{
                LayoutParams playerParams = new LayoutParams(PlayerComponent.LayoutParams.WRAP_CONTENT, PlayerView.LayoutParams.WRAP_CONTENT);
                PlayerComponent playerComponent = new PlayerComponent(this.getContext().getApplicationContext());
                playerComponent.setUserNameDisplay(playerName);
                playerComponent.setCountDisplay(balance);
                playerComponent.setLayoutParams(playerParams);
                addView(playerComponent);
            }catch(Exception e){
                Log.d("Ace2three","Exception while adding player");
                e.printStackTrace();
            }
        }
以下是自定义视图组中的onLayout方法

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
        int topPosition = 0;
        int leftPosition = 0;
        int rightPosition = 0;
        int bottomPosition = 0;
        int i;
        int j;
        int decrementX = 0;
        int incrementX = 0;
        int verticalGap = 75;
        int width;
        int height;
        View child;
        if( count > 0 ){
            int medium = count/2;
            int gap = getMeasuredWidth()/count;
            if( count == 1 ){
                child = this.getChildAt(0);
                width = child.getMeasuredWidth();
                height = child.getMeasuredHeight();
                leftPosition = getMeasuredWidth()/2 - width/2;
                rightPosition = getMeasuredWidth()  - ( getMeasuredWidth() - ( leftPosition + width ));
                topPosition = 10;
                bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - (topPosition + height));
                child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
            }
            else if( count%2 == 0 ){
                child = this.getChildAt(0);
                width = child.getMeasuredWidth();
                height = child.getMeasuredHeight();
                gap = getMeasuredWidth()/(count+1);
                decrementX = incrementX = getMeasuredWidth()/2 - width/2;
                for(i=medium,j=medium-1;(i<count && j>=0);i++,j--){
                    child = this.getChildAt(i);
                    width = child.getMeasuredWidth();
                    height = child.getMeasuredHeight();
                    decrementX -= gap;
                    leftPosition = decrementX;
                    rightPosition = getMeasuredWidth() - (getMeasuredWidth() - ( leftPosition + width ));
                    topPosition += verticalGap;
                    bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - ( topPosition + height ));
                    child.layout(leftPosition,topPosition,rightPosition,bottomPosition);

                    child = this.getChildAt(j);
                    incrementX += gap;
                    leftPosition = incrementX;
                    rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - ( leftPosition + width ));
                    child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
                }
            }
            else if( count%3 == 0 || count%3 == 2 ){
                child = this.getChildAt(medium);
                if( child.getVisibility() != GONE ){
                    width = child.getMeasuredWidth();
                    height = child.getMeasuredHeight();
                    topPosition = 10;
                    leftPosition = getMeasuredWidth()/2 - width/2;
                    bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - ( topPosition + height ) );
                    rightPosition = getMeasuredWidth() - (getMeasuredWidth() - (leftPosition + width));
                    child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
                    decrementX = leftPosition;
                    incrementX = leftPosition;
                }

                for(i=medium+1,j=medium-1;(i<count && j>=0); i++,j--){
                    child = this.getChildAt(i);
                    width = child.getMeasuredWidth();
                    height = child.getMeasuredHeight();
                    decrementX -= gap;
                    leftPosition = decrementX;
                    rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - ( width + leftPosition ));
                    topPosition += verticalGap;
                    bottomPosition = getMeasuredHeight() - ( getMeasuredHeight() - (height + topPosition));
                    child.layout(leftPosition,topPosition,rightPosition,bottomPosition);

                    child = this.getChildAt(j);
                    width = child.getMeasuredWidth();
                    height = child.getMeasuredHeight();
                    incrementX += gap;
                    leftPosition = incrementX;
                    rightPosition = getMeasuredWidth() - ( getMeasuredWidth() - (width + leftPosition));
                    child.layout(leftPosition,topPosition,rightPosition,bottomPosition);
                }
            }
protectedvoid only布局(布尔值已更改、int left、int top、int right、int bottom){
最终整数计数=getChildCount();
int-topPosition=0;
int leftPosition=0;
int rightPosition=0;
int-bottomPosition=0;
int i;
int j;
int递减=0;
整数递增x=0;
int垂直间隙=75;
整数宽度;
内部高度;
看孩子;
如果(计数>0){
int介质=计数/2;
int gap=getMeasuredWidth()/count;
如果(计数=1){
child=this.getChildAt(0);
width=child.getMeasuredWidth();
高度=child.getMeasuredHeight();
leftPosition=getMeasuredWidth()/2-宽度/2;
rightPosition=getMeasuredWidth()-(getMeasuredWidth()-(leftPosition+width));
顶置=10;
底部位置=getMeasuredHeight()-(getMeasuredHeight()-(顶部位置+高度));
子布局(左位置、上位置、右位置、下位置);
}
否则如果(计数%2==0){
child=this.getChildAt(0);
width=child.getMeasuredWidth();
高度=child.getMeasuredHeight();
间隙=getMeasuredWidth()/(计数+1);
DecrementTX=incrementX=getMeasuredWidth()/2-宽度/2;
对于(i=medium,j=medium-1;(i=0);i++,j--){
child=this.getChildAt(i);
width=child.getMeasuredWidth();
高度=child.getMeasuredHeight();
递减x-=间隙;
leftPosition=递减Tx;
rightPosition=getMeasuredWidth()-(getMeasuredWidth()-(leftPosition+width));
顶部位置+=垂直间隙;
底部位置=getMeasuredHeight()-(getMeasuredHeight()-(顶部位置+高度));
子布局(左位置、上位置、右位置、下位置);
child=this.getChildAt(j);
增量x+=间隙;
leftPosition=递增x;
rightPosition=getMeasuredWidth()-(getMeasuredWidth()-(leftPosition+width));
子布局(左位置、上位置、右位置、下位置);
}
}
else if(计数%3==0 | |计数%3==2){
child=this.getChildAt(中);
if(child.getVisibility()!=消失){
width=child.getMeasuredWidth();
高度=child.getMeasuredHeight();
顶置=10;
leftPosition=getMeasuredWidth()/2-宽度/2;
底部位置=getMeasuredHeight()-(getMeasuredHeight()-(顶部位置+高度));
rightPosition=getMeasuredWidth()-(getMeasuredWidth()-(leftPosition+width));
子布局(左位置、上位置、右位置、下位置);
递减Tx=左位置;
递增x=左位置;
}
对于(i=medium+1,j=medium-1;(i=0);i++,j--){
child=this.getChildAt(i);
width=child.getMeasuredWidth();
高度=child.getMeasuredHeight();
递减x-=间隙;
leftPosition=递减Tx;
rightPosition=getMeasuredWidth()-(getMeasuredWidth()-(宽度+左位置));
顶部位置+=垂直间隙;
底部位置=getMeasuredHeight()-(getMeasuredHeight()-(高度+顶部位置));
子布局(左位置、上位置、右位置、下位置);
child=this.getChildAt(j);
width=child.getMeasuredWidth();
高度=child.getMeasuredHeight();
增量x+=间隙;
leftPosition=递增x;
rightPosition=getMeasuredWidth()-(getMeasuredWidth()-(宽度+左位置));
子布局(左位置、上位置、右位置、下位置);
}
}

我不明白的是,它是第一次工作。但一旦我删除其中一个视图并尝试添加它,它就不工作了。

您是否实现了
onLayout
?是的……它是第一次从构造函数中工作的。但是当我删除一个视图并尝试再次添加它时,它就不工作了。您可以发布onLayout吗?黑带,请查看my问题。我已在视图组内使用onLayout方法更新了内容。我进一步调试了我的应用程序,碰巧看到onLayout仅在视图组内第一次调用。它不是第二次调用。