2016-11-22 47 views
0

我试图创建一个自定义视图,以取代一个xml /膨胀有一定布局,包含多个预定义的LinearLayout和添加这些看法到每一个预定义的布局。添加在RelativeLayout的意见,不同的布局,膨胀

我的预定义布局包含内部的RelativeLayout作为亲和充气此xml类4个linearlayouts那里延伸的RelativeLayout并如下初始化视图。

private void initView(Context context) { 
    //Inflate and attach your child XML 
    View.inflate(context, R.layout.layout_empty_views, this); 
    llTop = (LinearLayout) findViewById(R.id.linearLayout); 
    llLeft = (LinearLayout) findViewById(R.id.linearLayout2); 
    llBottom = (LinearLayout) findViewById(R.id.linearLayout3); 
    llRight = (LinearLayout) findViewById(R.id.linearLayout4); 
} 

现在,我接受12个子视图,需要添加3个孩子到每个linearlayout是我现在卡在哪里。在addView函数中由于索引而不能分割视图总是-1。

@Override 
public void addView(View child, int index, ViewGroup.LayoutParams params) { 
    if (llTop == null) { 
     super.addView(child, index, params); 
    } else { 
     //Forward these calls to the content view 
     llTop.addView(child, index, params); 
    } 
} 

在onLayout函数中,由于膨胀我的预定义布局,getChildCount被提高超过12。

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    //super.onLayout(changed, l, t, r, b); 
    // TODO Auto-generated method stub 

    Log.v("Count::", String.valueOf(getChildCount())); 
    /*for (int i = 0; i < getChildCount(); i++) { 
     View v = getChildAt(i); 
     removeViewAt(i); 
     //llTop.addView(v); 
     if (i < 3) { 
      llTop.addView(v); 
     } else if (i > 2 && i < 6) { 
      llRight.addView(v); 
     } else if (i > 5 && i < 9) { 
      llBottom.addView(v); 
     } else if (i > 9) { 
      llLeft.addView(v); 
     } 
    }*/ 
} 

回答

0

addView(View child, int index)接收的索引-1,这意味着它应该被添加到的ViewGroup的末尾。

我不知道我究竟了解你的意图与此视图,您可能不会在这里需要自定义视图。但是,我可以告诉你,你不应该在onLayout函数中添加视图。这个函数是View的大小和位置的子元素。

退房的Android开发者对creating custom views指南。

+0

是的,但我怎么动这个观点到另一个布局 – chiru