Android 将视图添加到自定义视图组

Android 将视图添加到自定义视图组,android,Android,我正在尝试将视图添加到自定义视图组。将绘制视图组,但添加到其中的视图不可见。无法调用LineView的(扩展视图的)onDraw()方法。我做错了什么 public class TestActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R

我正在尝试将视图添加到自定义视图组。将绘制视图组,但添加到其中的视图不可见。无法调用LineView的(扩展视图的)onDraw()方法。我做错了什么

 public class TestActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         ShapeView shapeView = new ShapeView(this);
         shapeView.setBackgroundColor(Color.RED);
         drawContainer = (RelativeLayout)findViewById(R.id.draw_container);
         drawContainer.addView(shapeView);   
    }
 }


 public class ShapeView extends ViewGroup {

     private LineView mLineView;

     public ShapeView (Context context) {
         super(context);
         RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(200, 200);
         this.setLayoutParams(p); 
         mLineView = new LineView(context);
         this.addView(mLineView);   
     }     
 }

我可能错了,但是你不应该把shapeView添加到布局“main”中吗?什么是抽屉?我看不到它被实例化,也看不到它在文档中。我假设main包含相对/线性布局

通过以下方式访问:

RelativeLayout rel = (RelativeLayout) findViewById(R.id.container);
(更改id以匹配您的id)

然后:


对不起,忘记将那行代码粘贴到问题上了。更新了。没关系,我以为是50/50。好的,那么你到底看到了什么?你有红场吗?或者你什么也没看到?我只看到了红场,别的什么都没有。我觉得我应该在onLayout()方法中添加一些东西,但我不确定它的用途以及如何正确编码。我在文档中也看不到LineView。这是你的另一门课吗?如果是这样的话,也许值得添加它。一旦我实现了ViewGroup的onLayout()方法,它就起作用了:)
rel.addView(shapeView);