我正在尝试将视图添加到自定义ViewGroup。将绘制ViewGroup,但不会显示添加到其中的视图。不会调用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);
}
}
答案 0 :(得分:0)
我可能错了,但你不应该将shapeView添加到你的布局“主”吗?什么是drawContainer?我看不到它被实例化而不是在文档中。我假设main包含相对/线性布局。
通过以下方式访问:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.container);
(其中id更改为与您的ID匹配)
然后:
rel.addView(shapeView);