我想一個視圖添加到一個ViewGroup上ViewGroup中的的onTouchEvent,以下視圖是代碼:
添加在一個ViewGroup
ViewGroup mDrawingCanvas = new ViewGroup(mContext)
{
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
Log.d(TAG, "View's On focused is called !");
return super.isFocused();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
View vv = new View(mContext);
vv.setBackgroundColor(Color.GREEN);
vv.setLayoutParams(new LayoutParams(200, 200));
this.addView(vv);
Toast.makeText(mContext, "Views child count = "+this.getChildCount(), Toast.LENGTH_SHORT).show();;
break;
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r,
int b) {
// TODO Auto-generated method stub
}
};
被添加到ViewGroup中來看,吐司顯示在每一個更大的瀏覽次數觸摸關閉事件,但ViewGroup中的屏幕上未顯示視圖。
我在StackOverflow上看到過相關的問題,但我的問題是獨一無二的。
請指導我如何解決這個問題,高度讚賞任何幫助。
P.S:我也嘗試在將View添加到ViewGroup之前設置視圖的Layout參數,但獲得的結果相同。在將ViewGroup添加到ViewGroup中之後,我還在LogCat中打印了視圖的寬度和高度,並且它始終顯示0.我認爲問題出現在視圖寬度和高度設置的某處。
實現onLayout方法 – pskink
請詳細說明一下,我應該在'onLayout(...)'方法做什麼實現? –
只是看到現有的ViewGroup.onLayout。實現 – pskink