1
我想將視圖添加到自定義ViewGroup。 ViewGroup被繪製,但沒有添加到它的視圖是可見的。 LineView的(擴展視圖)onDraw()方法不會被調用。我究竟做錯了什麼?將視圖添加到自定義ViewGroup
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);
}
}
對不起,忘了該行的代碼粘貼到問題。更新。 – KcYxA 2012-01-09 01:34:57
沒關係,我認爲是50/50。好的,你真的看到了什麼?你有紅色的方塊嗎?或者你什麼都看不到? – 2012-01-09 01:37:41
我看到紅色的方塊,沒有別的。我有一種感覺,我應該把一些東西放到我的onLayout()方法中,但我不確定它的用途以及如何正確編寫它。 – KcYxA 2012-01-09 01:40:27