我試圖在佈局中繪製多個自定義視圖,但只添加繪製的第一個視圖。繪製多個自定義視圖
我的自定義視圖類:
public ButtonView(Context context, int k) {
super(context);
this.setX(20+k*80);
init();
}
private void init(){
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0x99ffffff);
}
@Override
protected void onDraw(Canvas cv){
cv.drawText(""+getX(), 0, 80, paint);
}
我顯示的x文本協調,以便知道哪些圖繪。
這是我主要活動的onCreate
的代碼。
blay = (LinearLayout) this.findViewById(R.id.buttonslayout);
for(int k=0; k<10; k++){
ButtonView e = new ButtonView(this,k);
blay.addView(e);
}
當我運行應用程序時,只繪製第一個視圖。我在正確的位置獲得了以白色顯示的「20.0」,但其餘視圖未顯示。通過調試應用程序,我意識到視圖的實例已創建,但由於某些原因,它們不會顯示。此外,我試圖使用invalidate
並覆蓋onMeasure
方法,但仍然無法正常工作。